Safe maximum number of records read by 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 开发者_StackOverflow社区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?
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...
fread()
is not supposed to return a short item count unless a read error or end-of-file is reached. This sounds like a bug in the C library you're linking against (doesn't MinGW link against Microsoft's C library by default?).
精彩评论