Solved

question about fread

Posted on 2003-11-24
12
486 Views
Last Modified: 2008-03-17
In a file, I have the hex bytes:
0x00 0x08 0x08 0x09 0x00 0x00

When I try to get those 4 bytes using the following:
int hexBytes;
fread(&hexBytes,sizeof(int),1,f);

I would expect to the the following:
0x00080809

What I actually get though is:
0x9080800

First of all, why is it reading the bytes backwards?  Also, why does it only retreive 7 bytes instead of 8?
0
Comment
Question by:lwinkenb
  • 5
  • 5
  • 2
12 Comments
 
LVL 5

Author Comment

by:lwinkenb
ID: 9813681
err, I mean why does it only receive 3 and a half instead of 4 bytes :)
0
 
LVL 16

Accepted Solution

by:
imladris earned 150 total points
ID: 9813702
I assume you are using an Intel machine.

An intel machine is little endian. That is, the least significant byte is stored at the lowest address. So, if you have an integer with value 1, it is stored in memory as:

0x01000000

So, if you have the bytes

0x00 0x08 0x08 0x09,

on disk, an fread will transfer in that order into memory. Thus an integer will have the value

0x09080800

And the fread is getting 4 bytes, since that is the value for sizeof(int).
0
 
LVL 16

Expert Comment

by:imladris
ID: 9813706
Cross posted:

3 1/2 instead of 4:

The front 1/2 byte (nibble) is a 0. Leading 0's are often not shown.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Author Comment

by:lwinkenb
ID: 9813771
ok, just to be sure I understand:

If I performed the following:

char szText[32];
memset(szText,0,32);
strcpy(szText,"testing");
fwrite(szText,sizeof(char),32,f);

char szText2[32];
fread(szText2,sizeof(char),32,f);

Then this would work the way I want (with szText2 holding "testing") because it reads it byte per byte.

But anytime I read something that is bigger than a byte it will store it backwards?

What would happen if I did the follow?

struct TEST
{
int a,b;
float x;
};

TEST test;
fwrite(&test,sizeof(TEST),1,f);
//..
TEST test2;
fread(&test2,sizeof(TEST),1,f);

Would that do what I want, or would it fail?  Also is only intel machines little endian, or are AMD machines that way too?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9813773
increasing points too
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 9813831
>>Would that do what I want, or would it fail?

It actually should work.

>>Also is only intel machines little endian, or are AMD machines that way too?

All x86 machines are little endian...

BTW, what also should work is

int hexBytes = 0x00080809;

fwrite(&hexBytes,sizeof(int),1,f);

hexBytes = 0;

fread(&hexBytes,sizeof(int),1,f);

Alternatively, use the conversion functions that are made to circumvent this problem:

#include <winsock.h>

int hexBytes;
fread(&hexBytes,sizeof(int),1,f);

hexBytes = nothl ( hexBytes);



0
 
LVL 16

Expert Comment

by:imladris
ID: 9814078
Yes, any primitive larger than a byte is stored in little endian order. That includes int's and float's. The array of character would preserve the order you expect, because the underlying primitive (char) is a single byte.

And yes, AMD stuff is designed to be compatible with Intel hardware so it works the same way.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9814723
Why would my second example with the structs work?  They are bigger than a byte, so wouldnt they be stored in little endian order as well?  Or is it because they are not primitive values?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9815224
>>Why would my second example with the structs work?  

Because once you wrote them in little endian format (what 'fwrite()' actually does), you are being able to read them back in the same format and have the layout being interpreted correctly. Just as I wrote above:


BTW, what also should work is

int hexBytes = 0x00080809;

fwrite(&hexBytes,sizeof(int),1,f);

hexBytes = 0;

fread(&hexBytes,sizeof(int),1,f);

0
 
LVL 16

Expert Comment

by:imladris
ID: 9825384
Did any of those answers help?

If so, it is now time to select one (or more) and grade it to close the question.

If not, perhaps a clarifying question would help.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9836438
>> Imladris
I know how EE works :)  I just haven't been home the last few days due to visiting my parents for thanksgiving.
0
 
LVL 16

Expert Comment

by:imladris
ID: 9839083
Thanks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question