views:

302

answers:

3

I am reading in pieces of a binary file using a FILE object in C++. Here is the fseek and corresponding fread call:

fseek(fp, startLocation, SEEK_SET);
fread(data, m_sizeOfData, 1, fp);

m_sizeOfData ends up being an integer greater than 400 thousand. This appears that it should read all 400 thousand+ bytes from the binary file into data (which is a char[m_sizeOfData], by the way), however it stops after about 6 or 7 characters at a unicode character that simply looks like a box. I'm thinking it might represent a null termination? I'm not positive on this. This isn't the case with every piece of the file that I am reading in. Most seem to work (generally) correctly.

Why might this be and is there a way to correctly read all of the data?

Edit:

fp is defined as such:

FILE* fp;
_wfopen_s(&fp, L"C://somedata.dat", L"rb");

This box character, in hex, is 0x06 followed by 0x00.
The data is defined by: char *data = new char[m_sizeOfData];

edit 2:

I've also noticed that another file is having some garbage loaded onto the end of it. The garbage looks like:

ýýýý««««««««îþ

Is this because it is trying to complete a certain round number of bytes?

+1  A: 

How do you know it's stopping where you say? If you're just looking at the result with string functions, those string functions will all stop at the first null character - the actual data might extend much much further.

Mark Ransom
I'm inspecting the data passed into the char* from the debugger directly after fread is called.
Chris
Then can you tell us the hex equivalent of this "box" character? And how do you know the data stops there?
Mark Ransom
how are you inspecting it?
Keith Nicholas
it's 0x06 followed by 0x00
Chris
A: 

If you are on windows, I think there are some chars that like ctrl-Z or ctrl-D that can signify end of file unless you specifically open the file in binary mode

gnibbler
FILE* fp; _wfopen_s( this is opening it in binary mode, correct?
Chris
yes the b is for binary. can you view the file in a hex editor?
gnibbler
i can view the file in notepad, although i don't have a hex editor.
Chris
+1  A: 

You are using the count/size parameters of fread the wrong way around. Since you are reading bytes, the second parameter should be 1 and the third parameter the count:

fread(data, 1, m_sizeOfData, fp);

You can then use the return value of fread to determine how many bytes were read. If you are getting the expected count returned, then you can be comfortable that you are reading all the data you wanted. In this case, you are probably outputting the data incorrectly - if you are treating it as a NUL-terminated string, then the 0x00 you are seeing will be the end of what is printed. The 0x06 is probably the box glyph.

camh
thanks for your response. this yields the same result. i suppose it is possible that the "Value" field in the debugger in VS null terminates the string, but I'm still pulling errors from it. I've noticed that another piece being loaded in has some garbage (ýýýý««««««««îþ) at the end of it..
Chris
I'd like to emphasize part of this answer, "use the return value of fread" - this will tell you exactly how many bytes were read from the file. Don't trust anything past that point.
Mark Ransom