Questions tagged [fread]

A binary-safe file read function in C/C++/PHP that returns the specified number of bytes from a stream. Also a fast csv parser in R's data.table package.

References:

1613 questions
7
votes
3 answers

Why does ftell() shows wrong position after fread()?

I'm getting a very strange error while trying to read from a simple text file with c fread() call. I made a very simple program to show that error: int main(int argc ,char ** argv) { FILE* fh = fopen("adult.txt","r"); if(fh==NULL){ …
ezzakrem
  • 399
  • 5
  • 15
7
votes
4 answers

How to read line by line after i read a text into a buffer?

First , I read a text into a buffer by calling fread, and then I want to read it line by line, how to do it? I try to use a sscanf , but it seems not to work. char textbuf[4096]; char line[256]; FILE *fp; fp = fopen(argv[1],"r"); memset(textbuf, 0,…
Fei Xue
  • 1,995
  • 5
  • 19
  • 29
6
votes
5 answers

ifstream vs. fread for binary files

Which is faster? ifstream or fread. Which should I use to read binary files? fread() puts the whole file into the memory. So after fread, accessing the buffer it creates is fast. Does ifstream::open() puts the whole file into the memory? or does…
Snowfish
  • 7,479
  • 4
  • 20
  • 20
6
votes
2 answers

R data.table fread: specify column data type

Is it possible to specify the data type of numeric columns in fread? Commands like prices = markets[, fread(paste(mkt, 'price.csv')), by = mkt] will fail if price data are integers in one file and floats in another. So instead of a clean one-liner,…
jf328
  • 6,841
  • 10
  • 58
  • 82
6
votes
2 answers

Fastest method to deal with string

I have a text file to read and deal with with 20000 lines. In the text file I want to read the point coordinates and assign to DirectX to render.Snapshot of Text file I have used std::ifstream, getline, stringstream to get the point coordinates.…
timhu
  • 63
  • 4
6
votes
2 answers

fread to read top n rows from a large file

I am getting below error when reading first n rows from a big file(around 50 GB) using fread. Looks like a memory issue. I tried to use nrows=1000 . But no luck. Using linux file ok but could not memory map it. This is a 64bit process. There is…
sjd
  • 1,329
  • 4
  • 28
  • 48
6
votes
0 answers

Read in data in chunks via fread in R

I am trying to read a huge dataset (> 25GB) in R. It doesn't fit in my PCs memory. But I think maybe it would be possible to work with the data, if it were in compressed (RData) format. As part of the process, I also have to change column classes to…
EDC
  • 613
  • 2
  • 7
  • 16
6
votes
2 answers

C: Reading a text file (with variable-length lines) line-by-line using fread()/fgets() instead of fgetc() (block I/O vs. character I/O)

Is there a getline function that uses fread (block I/O) instead of fgetc (character I/O)? There's a performance penalty to reading a file character by character via fgetc. We think that to improve performance, we can use block reads via fread in the…
6
votes
3 answers

PHP - read version number in composer.json

How do I make a script there can tell me what version i run when it stored in composer.json? composer.json { "require": { "someLiberyNameHere": "8.3.3.1" } }
Budget
  • 61
  • 1
  • 3
6
votes
6 answers

How to interrupt a fread call?

I have the following situation: There is a thread that reads from a device with a fread call. This call is blocking as long as there is no data send from the device. When I stop this thread it remains hanging inside this thread. Now I found the…
Peter Fortuin
  • 5,041
  • 8
  • 41
  • 69
6
votes
1 answer

Use of fread() from data.table causes R session to abort

I am working on a project for a MOOC, and was tinkering around with the data.table package in RStudio. Use of the fread() function to import the data files initially worked fine: fread("UCI HAR Dataset/features.txt")->features fread("UCI HAR…
GH28
  • 135
  • 1
  • 12
6
votes
2 answers

PHP fastest method of reading server response

I'm having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls I'm making. I'm sending through a batch of say, 10,000 calls and I've tracked the lag down to fgets causing the hold up in…
Peter John
  • 1,859
  • 4
  • 15
  • 14
6
votes
2 answers

fread dropping carriage returns in C?

I want to be able to read in a Windows text file, modify it in memory, and then overwrite the old file with the modified data. However, fread doesn't seem to store the carriage returns present in my Windows text file, which is throwing things off…
thingyman
  • 65
  • 1
  • 3
6
votes
2 answers

Can i use fgetc() or fputc() in a binary file?

I am creating an archive program in C, and i want it to save files i provide, list and extract them. I had many issues because i used a text file for saving, and it is not the best choice if i want to process binary files like music or photos,…
Andrea Gottardi
  • 357
  • 2
  • 3
  • 21
6
votes
5 answers

C++ fread() into a std::string

Like always, problems with pointers. This time I am trying to read a file (opened in binary mode) and store some portion of it in a std::string object. Let's see: FILE* myfile = fopen("myfile.bin", "rb"); if (myfile != NULL) { short stringlength…
ali
  • 10,927
  • 20
  • 89
  • 138