fread problems

Started by
9 comments, last by popcorn 18 years, 8 months ago
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?
How about them apples?
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?
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().
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)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
Or you can use "fscanf":

fscanf(file, "%i", &number);

after opening the file with the "r" flag (not "rb").
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
How about them apples?
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.
______________________TradeMark Designs
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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

This topic is closed to new replies.

Advertisement