Solved

question about fread

Posted on 2003-11-24
12
484 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now