fread

How to read giant text file in PHP? [duplicate]

佐手、 提交于 2019-12-11 04:56:57
问题 This question already has answers here : Reading very large files in PHP (8 answers) Closed 5 years ago . I have few text files with size more than 30MB . How can i read such giant text files from PHP? 回答1: Unless you need to work with all the data at the same moment, you can read them in pieces. Example for binary files: <?php $handle = fopen("/foo/bar/somefile", "rb"); $contents = ''; while (!feof($handle)) { $block = fread($handle, 8192); do_something_with_block($block); } fclose($handle);

Why does moving the buffer pointer slow down fread (C programming language)?

牧云@^-^@ 提交于 2019-12-11 03:23:15
问题 I am reading a 1 GB file using fread in C. I am reading the file in 1MB chunks, using the following loop: FILE *fp; fp = fopen(filename, "rb"); unsigned char* buf; buf = malloc(CHUNK_SIZE); for(i = 0; i < NUMBER_OF_CHUNKS; ++i) { fread(buf, CHUNK_SIZE, 1, fp); //Do something with contents of buffer } fclose(fp); Reading the file this way takes ~2 seconds. However, I decided that I wanted to allocate one big buffer for the contents of the whole file instead and "move the buffer pointer" inside

C reading from a live file ( file keep growing in size )

╄→гoц情女王★ 提交于 2019-12-11 01:54:23
问题 I have an application that is recording live , the capture file keep growing in size using fread() and feof() , but feof() is breaking the loop early , so what's the best technique to keep reading from the stream should I wait and then I can advance the file stream ? should I open the file again and advance to position by calculating the total of read bytes? maybe something else ? the code will have to read the file , build a packet and send it packaging and sending is going well with fixed

R data.table fread using named colClasses without header (e.g. no col.names?)

我怕爱的太早我们不能终老 提交于 2019-12-10 17:55:37
问题 update (June 2016) col.names was added on data.table 1.9.6 so issue is over and everyone super happy :) I think I can now convert all my read.csv calls to fread calls without worries of destruction original question using data.table 1.9.4 I'm importing read.csv calls to fread due to HUGE performance improvements we've noticed. Most issues I can handle but I've reached a point where I'm clueless and wonder if anyone has an elegent solution. My problem is that I have named colClasses but the

Replacing a line in a file without rewriting the entire file (in PHP)

放肆的年华 提交于 2019-12-10 17:55:30
问题 Lets say I have a modestly sized text file (~850kb, 10,000+ lines) And I want to replace a particular line (or several) spread out amongst the file. Current methods for doing this include re-writing the whole file. The current method I use is read through the entire file line by line, writing to a .tmp file, and once I am done, I rename() the tmp file to the original source file. It works, but it is slow. And of course, as the file grows, so will execution times. Is there another way (using

What's the best way to read from and then overwrite file contents in php?

浪尽此生 提交于 2019-12-10 17:32:04
问题 What's the cleanest way in php to open a file, read the contents, and subsequently overwrite the file's contents with some output based on the original contents? Specifically, I'm trying to open a file populated with a list of items (separated by newlines), process/add items to the list, remove the oldest N entries from the list, and finally write the list back into the file. fopen(<path>, 'a+') flock(<handle>, LOCK_EX) fread(<handle>, filesize(<path>)) // process contents and remove old

How to use fread() with “https” url scheme?

家住魔仙堡 提交于 2019-12-10 14:32:56
问题 In R Documentation for fread() it's said that "input" argument may be a URL starting http:// or file://. However in this "Introduction to data.table" vignette fread() is used with https: flights <- fread("https://raw.githubusercontent.com/wiki/arunsrinivasan/flights/NYCflights14/flights14.csv") That results in "Error in download.file(input, tt, mode = "wb") : unsupported URL scheme" console message. I can't figure out how to make it work. I've tried settings options(download.file.method =

fread: read certain row as implicitly ordered factor

一笑奈何 提交于 2019-12-10 11:54:35
问题 I am fairly new to R , and have been using data.table a lot recently for a project involving manipulation of large data sets, specifically genome data. One of the columns is the chromosome number/name, which is formatted as "chr_", where the _ is 1-22, X, or Y. As the data is sorted by chromosomal position, this is a natural primary key for my data. However, setting this as the key produces unwanted results, namely sorting by lexicographic order rather than general numeric order (i.e. the

How to convert jpg image to proper blob data type using php

Deadly 提交于 2019-12-10 10:48:00
问题 <?php $file_name = $_FILES['files']['name']; $tmp_name = $_FILES['files']['tmp_name']; $file_size = $_FILES['files']['size']; $file_type = $_FILES['files']['type']; // The codes written above work fine and have proper information. $fp = fopen($tmp_name, 'r'); // This one crashes. $file_content = fread($fp, $file_size) or die("Error: cannot read file"); $file_content = mysql_real_escape_string($file_content) or die("Error: cannot read file"); fclose($fp); .... I'm a newbie to PHP stuff. I'm

C fread() magically reading dynamically allocated struct members, how?

喜你入骨 提交于 2019-12-10 03:52:17
问题 This is a test program that I have written for a larger project that I am working on. It has to do with writing struct data to disk with fwrite() and then reading that data back with fread(). One member of the struct is dynamically allocated. First, here is my code #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRING_LEN 128 struct Person { int age; char *name; }; int main(int argc, const char *argv[]) { struct Person *person = calloc(1, sizeof(struct Person)); person-