Solved

question about fread

Posted on 2003-11-24
12
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Technology Partners: 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
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.

756 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