Reputation: 153
I am using fread() in C++ to read very large binary files (100MB-2GB). The binary files are originally written from C++ by outputting a series of "packets". The packets are made of a "header" struct (that contains a size field) being directly written to a file, and then binary content with size equal to the size written in to the header. When reading the files, the packets are looped over, the header is read in to a struct and the content is read in to a void pointer of the size provided in the header.
This is a known working method already implemented in other tools (meaning I can validate the files I am trying to read). Assume all files we are working with are validated. In at least one file, my implementation of reading a binary file is working correctly.
However, with another file fread() starts acting funky for no apparent reason. After many successful reads, I cleanly read the header portion of a packet using:
if (sizeof(stHdr) != fread((void *)&stHdr, 1, sizeof(stHdr), fi))
By cleanly reads, I mean fread() returns "sizeof(stHdr)" as expected, and feof(fi) and ferror(fi) both return 0. However... stHdr is completely filled with all zeros; the value of every field in stHdr contains 0x0. I have validated the binary file to be correctly formed, and to have data at the point that I am reading.
Has anyone seen this before or know what could be causing it?
Thanks!
Upvotes: 0
Views: 917
Reputation: 153
The problem ended up being a classic case of PEBKAC...
Apparently my binary file did become corrupted at some point and actually did have a bunch of 0s in it. I had copied it directly out of a repository and the file was validated before being put in to the repo, so I assumed it was good. Apparently something bad happened to my local version of the file and was the source of my problems.
Upvotes: 0
Reputation: 7838
If your files are over 2GB, you'll need to enable large file support.
The quick and easy way to do this is to compile with -D_FILE_OFFSET_BITS=64
. For the more targeted ways, and more details, see http://www.suse.de/~aj/linux_lfs.html
Upvotes: 0