Re: Binary file-question!!!
Your file is in plain-text format but you are reading it as if it is a binary-coded format. That's why you get what looks to be junk.
Re: Binary file-question!!!
so...what could I change???
Re: Binary file-question!!!
Depends upon how it should work :). Either:
1) Change the code to read the file as plain-text, or
2) Change the file so the data is binary-coded.
Re: Binary file-question!!!
My file is binary-coded,I had written this code:
Code:
#include <stdio.h>
struct info
{
float mo;
int age;
}students[5];
int main()
{
FILE *fp;
int i;
for (i=0; i<5; i++){
scanf("%f%d",&students[i].mo,&students[i].age);
}
fp=fopen("sx.txt","wb");
fwrite(students,sizeof(struct info),5,fp);
fclose(fp);
return 0;
}
:/
Re: Binary file-question!!!
Why does it not give the right output???
Re: Binary file-question!!!
Code:
int main()
{
int il;
FILE *fp;
fp=fopen("sx.txt","rb");
if (fp==NULL){
printf("Problem!!!\n");
exit(0);
}
fseek(fp,4,0);
fread(&m,sizeof(struct info),1,fp);
printf("Age:%d\n",m.age);
rewind(fp);
fread(&m,sizeof(struct info),1,fp);
printf("Age:%d\n",m.age);
return 0;
}
Look at the '4' in the fseek call. You should be seeking a multiple of sizeof(info).
Edit: And don't give files a 'txt' extension unless they are human-readable. It only confuses things
Re: Binary file-question!!!
Nice!!!!Thank you very much!!!!!!! :)