文件操作
# include <stdio.h># include <string.h>
struct student
{
int ID;
char name[32];
int age;
char sex[4];
};
int main (void)
{
int i = 0 ;
struct student a , b , *pb;
scanf ("%d %d %s %s" , &a.ID , &a.age , a.name , a.sex);
while (a.age > 0)
{
FILE * p = fopen ("Amyfile.txt" , "a+");
if (p == 0)
printf ("打开文件失败……");
fprintf (p , "%d %d %s %s\n" ,a.ID , a.age , a.name , a.sex);
scanf ("%d %d %s %s" , &a.ID , &a.age , a.name , a.sex);
fclose (p);
}
FILE * p = fopen ("Amyfile.txt" , "r+");
if (p == 0)
printf ("打开文件失败……");
rewind (p);
pb = &b;
clearerr (p);
while (!feof (p))
{
fscanf (p , "%d %d %s %s" , &pb->ID , &pb->age , pb->name , pb->sex);
printf ( "%d %d %s %s" , pb->ID , pb->age , pb->name , pb->sex);
printf ("\n");
}
fclose (p);
return 0;
}
这个为什么到最后总是会崩掉