Safe maximum number of records read by fread

I use fread to read a large chunk of image data (> 1 MB) from a file. I recently encountered a bug in MinGW with windows network resources where one call for fread as

fread(file, 4, 100000, data); 

with a reliable error with the error "Invalid argument", but 10 calls

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

succeed and give the right result. I conclude that there should be a maximum size for fread that I did not know about before. I split the allowed fread size and found that it is between 31000 and 32000 blocks of 4 bytes. Has anyone come across this before? Is this a bug in MinGW? Is there a way to determine the maximum "safe" size for fread?

+6
source share
2 answers

There is a known bug in MSVCRT (Microsoft Visual C Runtime used by mingw) that fread (and possibly also underlying _read or something else ??) fails with moderately long read lengths. You can break the reading into smaller parts, write your own version of fread to replace the system one (but do this only when compiling on broken systems!) Or switch to a better runtime (for example, cygwin), which is not full of errors ...

+7
source

fread() should not return a short item counter unless a read error or end of file is reached. This sounds like an error in the C library that you are linking to (is the MinGW link linked to the Microsoft C library by default?).

+1
source

Source: https://habr.com/ru/post/896192/


All Articles