I want to count the amount of "items" in binary file using feof and fseek functions. I have a binary file with names of people in chars and their salaries in floats. before each name there is an int that represents the amount of chars in the name. for example my file could look like this (but without spaces and without \0's): 5danny5000.00 4lena2500.50
one item is "4lena2500.50" for example.
in my code, the while loop does not stops. what can I do to repair the problem?
thanks!
int count_items(FILE* file)
{
int count=0;
int curr_name_len;
while (!feof(file))
{
fread(&curr_name_len, sizeof(int), 1, file);
fseek(file, (curr_name_len * sizeof(char)) + sizeof(float), SEEK_CUR);
count++;
}
rewind(file);
return count;
}
feofdoesn't check whether the file is at the EOF, it checks whether the eof-indicator of the file was set on a previous operation.fseekallows to seek to an arbitrary position (if the operating system and the file system supports this) to allow for example to write with holes inside of the file, which is useful if you intend to write things inbetween. Thus the eof-indicator is set after thefread-call, but is cleared after yourfseek-call. So this should work:or if you don't like that style:
or less structured, but more clearly: