Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 476 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C reading binary files

#1
> **Possible Duplicate:**
> [ “while( !feof( file ) )” is always wrong](

[To see links please register here]

)

<!-- End of automatically inserted text -->

If I write an array to the output file and I close the file, then open the file again and read everything until end-of-file is reached, although the file contains only 4 number, the program will read and print 5 numbers, why?

Program output:

a[0] = 4
a[1] = 7
a[2] = 12
a[3] = 34
a[4] = 34

save.bin (with a hex editor)

04000000 07000000 0C000000 22000000

------

#include <stdio.h>
#include <stdlib.h>
#define path "save.bin"

int main(void)
{
FILE *f=NULL;
int a[]={4,7,12,34},i,n=4,k;
f=fopen(path,"wb");
if(f==NULL)
{
perror("Error");
exit(1);
}
for(i=0;i<n;i++) // or I could use fwrite(a,sizeof(int),n,f);
fwrite(&a[i],sizeof(int),1,f);
fclose(f);
f=fopen(path,"rb");
if(f==NULL)
{
perror("Error");
exit(1);
}
i=0;
while(!feof(f))
{
fread(&k,sizeof(int),1,f);
printf("a[%d] = %d\n",i,k);
i++;
}
printf("\n");
fclose(f);
return 0;
}
Reply

#2
while(!feof(f))
{
fread(&k,sizeof(int),1,f);
printf("a[%d] = %d\n",i,k); /** THIS ASSUMES fread() WAS SUCCESSFUL. **/
i++;
}

Check for end of file immedately after the call to `fread()` or check the return value of `fread()`, which returns the number of items read which in this case should be `1`. A possible restructuring of the loop:

while(1 == fread(&k,sizeof(int),1,f))
{
printf("a[%d] = %d\n",i,k);
i++;
}

After the loop check `feof()` to ensure EOF was reached and loop did not end due to some other failure.
Reply

#3
I have the same problem as well. Try this instead:

i=0;
while(true)
{
fread(&k,sizeof(int),1,f);
if(feof(f))
break;

printf("a[%d] = %d\n",i,k);
i++;
}

Apparently feof doesnt return true unless you've read past the end of the file. What i do when writing arrays to binary files is to write the size first, so i know the exact amount of data i'm about to read when reading it back.
Reply

#4
`feof(fp)` becomes false (i.e. non-zero value) only if you tried to *read past* the end of file. That should explain why the loop is entered one more than what you expect.


From the [documentation][1]:

The function feof() tests the end-of-file indicator for the stream
pointed to by stream, returning nonzero if it is set. The end-of-
file indicator can be cleared only by the function clearerr().

[1]:

[To see links please register here]


Also read the post:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through