Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe maximum number of records read by fread

Tags:

c

mingw

fread

I am using fread to read a large chunk of image data (> 1 MB) from a file. I recently encountered a bug on MinGW with Windows network shares where a single call to fread like

fread(file, 4, 100000, data);

fails reliably with an "Invalid argument" error, but 10 calls of

fread(file, 4, 10000, data); data += 10000;

succeed and yield the right result. I deduce there must be a maximum size for an fread, which I was not aware of before. I bisected the allowed size of fread and found it to be between 31000 and 32000 blocks of 4 bytes. Has anyone encountered this before? Is this a bug in MinGW? Is there any way to determine the maximum "safe" size for fread?

like image 839
thiton Avatar asked Aug 30 '11 09:08

thiton


1 Answers

It's a known bug in MSVCRT (the Microsoft Visual C Runtime, which mingw uses) that fread (and perhaps also the underlying _read or whatever..?) fails on moderately long read lengths. You can either break the read down into smaller parts, write your own version of fread to replace the system one (but only do this when compiling on broken systems!), or switch to a better runtime environment (like cygwin) that's not full of bugs...

like image 116
R.. GitHub STOP HELPING ICE Avatar answered Oct 04 '22 05:10

R.. GitHub STOP HELPING ICE