- 论坛徽章:
- 0
|
各位大虾帮帮忙,我已经不行了
我在文件读写的时候总是不对,比如:
add_new()
{int count;
char flag='y';
struct student{int code;
char name[20];
int age;
};
struct student study;
FILE*fp;
if((fp=fopen("item","a+" ==NULL))
{ getchar();
exit(0);
}
int count;
fread(&count,sizeof(int),1,fp);
fwrite(&count,sizeof(int),1,fp);
while(flage=='y')
{count++;
printf("enter code,name,age:"
scanf("%d\t%s\t%d\t",&study.code,study.name,study.age);
fwrite(&study,sizeof(struct student),1,fp);
printf("\n add another?" ;
scanf("%c",flag);
}
rewind(fp);
fwrite(&count,sizeof(int),1,fp);
fclose(fp);
if((fp=fopen("item","a+" ==NULL))
{ getchar();
exit(0);
}
int count_1;
fread(&count_1,sizeof(int),1,fp);
printf("%d",count_1);
fseek(fp,sizeof(struct student)*2,1);
fread(&study,sizeof(struct student),1,fp);
printf("%d\t%s\t%d\t",study.code,study.name,study.age);
}
输出结果总是不对,各位大虾帮忙看一下 |
|