Solved

question about fread

Posted on 2003-11-24
12
485 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

895 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

12 Experts available now in Live!

Get 1:1 Help Now