Safe maximum number of records read by fread
Asked Answered
S

2

7

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?

Stricken answered 30/8, 2011 at 9:29 Comment(4)
That's pretty strange, especially since EINVAL is not a documented error code for fread. How are you doing your error checking?Sabayon
Is file a FILE *? because the first argument to fread is the buffer, not the file.Enduring
Hasturkun: Sorry, garbled my arguments. file is a float* and data is a FILE*.Stricken
I am checking the existence of an error with ferror() and getting the error message via strerror() (and perror() gives the same result).Stricken
D
9

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...

Delphinus answered 30/8, 2011 at 12:34 Comment(1)
Compiling on broken systems? Do you mean compiling for broken systems?Godden
P
2

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?).

Pachyderm answered 30/8, 2011 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.