功能:向文件stu中写入一条记录并重新读到屏幕上. #include <stdio.h> #define SIZE 100 typedef struct {char s_name[10]; long s_id; }STUDENT;
void main () { STUDENT stud[SIZE],astud; FILE *fp; int count;/*count of records*/ count=0;
printf("\nNAME:");scanf("%s",astud.s_name); printf("\nID:");scanf("%ld",&astud.s_id);
fp=fopen("stu","wb"); fwrite(&astud,sizeof(STUDENT),1,fp);/*write one record to 'stu' */ fclose(fp); fp=fopen("stu","rb"); while (!feof(fp))/*read all the records and print on the screen*/ {fread(&stud[count],sizeof(STUDENT),1,fp); printf("%-10s%-12ld\n",stud[count].s_name,stud[count].s_id); count++; } printf("count=%d",count); fclose(fp); 为什么我输入一个记录,却会打印两条,后一条为随机数,count显示为2. 是不是fread函数用错了,请问fread后文件指针fp中的位置项会指向哪里,是记录头还是尾? 谢谢!!!!!!