Jump to content
  • Advertisement
Sign in to follow this  
popcorn

fread problems

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a text file with the number 1234 and then I use the following code I seem to get strange results. FILE *file; int number = 0; file = fopen("new.txt", "rb"); fread(&number, sizeof(int), 1, file); printf("number is %d\n", number); I get something that doesn't correspond to the number in the text file. Why?

Share this post


Link to post
Share on other sites
Advertisement
Is the number in binary or in ASCII? If it is binary, is it the same endianess as the machine you're working on?

Share this post


Link to post
Share on other sites
If it's a text file, then you need to read the number as text and then transform the text into a integer. Just using "rb" won't magically read it as an int. Since this appears to be C code, one way to transform the string into an int would be to use atoi().

Share this post


Link to post
Share on other sites
Because you are trying to read binary data, not text data.

Assuming that an integer is 4 bytes, your code will be loading the bytes 49, 50, 51, 52 (the character values of the digits '1', '2', '3', '4') into the memory occupied by the integer. Assuming a little-endian (e.g. Intel) CPU, 49 would go into the least significant byte, so the resulting integer would be:

49 + 50*28 + 51*216 + 52*224 = 875770417

(thankfully the last byte is small enough that we don't have to worry about signedness)

Share this post


Link to post
Share on other sites
Oh sorry I missed the part where you say its a text file. You are trying to read a binary value instead of an ASCII value. You might be able to do something like


char buf[1024];
fread(buf,1024,1,file);


instead.

Share this post


Link to post
Share on other sites
Fruny thats exactly the number I get on my machine(AMD).

Ok I guess the options then would be:
1. Read the number in as a string and then convert to int
2. Use a different function to read in the data(fscanf)
3. Create the file as a binary file

Share this post


Link to post
Share on other sites
If you had just gone into a text editor and typed the numbers in, It would be stored as characters. The way around it would be to wip up a small program allowing you to write the integers directly into binary into the file like stated above.

Share this post


Link to post
Share on other sites
Quote:
Original post by popcorn
Fruny thats exactly the number I get on my machine(AMD).


Yes, AMD CPUs also are little-endian, meaning that when a number is bigger than a single byte, the least significant bytes come first in memory. Little-end first.

Quote:
Ok I guess the options then would be:
1. Read the number in as a string and then convert to int
2. Use a different function to read in the data(fscanf)
3. Create the file as a binary file


Take your pick. [smile] You're the one writing the program, you're the one who must decide which solution and tradeoffs suit you best.

Share this post


Link to post
Share on other sites
I see that you have gotten an answer as far as how to convert the text to int. It occurrs to me that a few words about the why and how of file IO as it applies to game development is in order...

Disk files are frequently used in games to store state or configuration information. When designing the part of the game where information is saved to disk you would need to choose between binary and text file type. If you choose binary you would create one or more file layouts (using struct) and use fread and fwrite for IO. On the other hand if you choose text as your file type you would organize data into newline terminated records and use fgets and fputs (or fprintf) for IO.

In many cases binary files are preferred over text because the data is usable as soon as it is read. The code for reading and writing binary files is usually very clean and logical. Text config files, on the other hand, are preferred when you want the file to be editable by the user.

-Rog

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!