|
From: P.D. v. Z. <pd...@xs...> - 2003-02-21 22:36:14
|
"sizeof file" does not do what you expect it to do. "sizeof" returns the
size of a type or an object, so in your case it returns the size of the
"FILE" struct, not the size of the file you opened.
To correctly loop through the file, you'd use something like:
while (!feof(f) && !ferror(f))
{
fread(&j, sizeof(unsigned int), 2, f);
// the fseek() is not necessary
// if you want to display what you've read from the file, I'd use a
printf() call or similar here
}
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of airam
Sent: Friday, February 21, 2003 11:16 AM
To: dev...@li...
Subject: [Dev-C++] fread
Hi!!!!!!
i want to read a file every 4bytes
and after printf
this is the code but doesn't work!!
#include <stdio.h>
int main()
{
unsigned int i,j,k;
FILE *f;
f=fopen("filename","r");
if (!f)
return 1;
fseek(f,0,SEEK_SET);
for (i=0;i<sizeof file; i++)
{
fread(&j,sizeof(unsigned int),2,f);
fseek(f,sizeof(unsigned int),SEEK_CUR);
}
fclose(f);
printf("\n");
}
|