Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

question about fread

Posted on 2003-11-24
12
Medium Priority
?
493 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 600 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

885 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