Click here to Skip to main content
15,903,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello experts,

I am a beginer using ansi C. I am quite used to use text files, but now i am trying to use a bin file.

I managed to write to the bin file, and even to read using a for loop, but is it possible to read till end of file in bin please?

This is my sample code.

C#
typedef struct
{
     char Name[name_l];
     short Score;
}player_t;

player_t Player1;

FILE *HighScores;
HighScores = fopen("HighScores.bin","rb");

fread(&Player1, sizeof(player_t),1,HighScores);
printf("\n%s\n",Player1.Name);
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-13 12:51pm    
What's a bin file? All files are binary, even the text files, do you understand that? :-)
And what's the problem?
—SA
Gilmore Sacco 30-May-13 13:02pm    
Hmm ok sir, i am not that expert.
by Bin file i understand that i open the file with "rb" not "r" like a text file
Sergey Alexandrovich Kryukov 30-May-13 13:16pm    
No. It does not make the "text file" special. You can open text file in both ways. "Text file" simply means that its content is oriented for human reading, and it does not contain anything unreadable, but this also depends on text encoding. Importantly, Unicode text files may or may not have BOM (Byte Order Mark) in the very beginning:
http://en.wikipedia.org/wiki/Byte_order_mark
Except for that, there are no rules.

In other words, all files are binary, but no all files can be called "text files".

—SA
Sergey Alexandrovich Kryukov 30-May-13 13:17pm    
Anyway, please see my answer. I think you should accept it formally (green button), as it fully resolves your issue.
In all cases, your follow-up questions are always welcome.
—SA
Gilmore Sacco 30-May-13 13:22pm    
thanks for the quick reply and sry foe my slow internet connection. i am trying this but it's not working..
while (fread(&Player1, sizeof(player_t),1,HighScores) != EOF)
{
printf("\n%s\n",Player1.Name);
}

1 solution

Just in case: if by "EOF" you mean the End-of-file character, this is just quite a stupid historical character with is still preserved but almost never used, because there are no reasons for using it. Please see:
http://en.wikipedia.org/wiki/End-of-file[^].

The ways to read the file to the end totally depend on the structure you need to read. If the structure is well developed, and if you have to read all the available instances of such structure, you always know either the number of those instances, or you can calculate them from the total size of the file, or you can read complex variable-size structures sequentially, but know the exact size of the last chunk of data from the data you already have.

But suppose you have more sloppy situation, when you cannot predict if you are trying to read behind the actual end of file or not. In this case, you can figure out it from the result returned from fread:
http://www.cplusplus.com/reference/cstdio/fread/[^].

(Don't get confused by this C++ documentation: it's also applicable to C.)

As you can see, it returns the total number of elements successfully read. If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading.

That's it.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900