I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the l
A solution, based on the previous answers:
size_t bytesRead = 0;
BYTE* localBuffer = new BYTE[bytesTotal];
fseek(stream,headerLen,SEEK_SET);
while(!feof(stream) && !ferror(stream)) {
size_t result = fread(localBuffer+bytesRead,sizeof(BYTE),bytesTotal-
bytesRead,stream);
bytesRead+=result;
}
perhaps it's a binary mode issue. Try opening the file with "r+b"
as the mode.
EDIT: as noted in a comment "rb"
is likely a better match to your original intent since "r+b"
will open it for read/write and "rb"
is read-only.
Also worth noting that simply including binmode.obj into your link command will do this for you for all file opens.