# include <stdio.h>
struct person
{
char name[32];
int phone[11];
char relation[32];
};
int main (void)
{
int i;
struct person a , b , *p2;
FILE *p = fopen ("person.txt" , "ab+");
if (p == 0)
return 0;
p2 = &a;
scanf ("%s" , p2->name);//因为name 是字符串,首字母就是地址,p2也是一个地址,所以这里可以省略&
for (i = 0; i < 11; i++)
scanf ("%d" , &a.phone[i]);
scanf ("%s" , &a.relation );//a是变量,不是指针,需要用&;
while (a.phone[0] > 0)
{
fwrite (p2 , sizeof (struct person) , 1 , p);
scanf ("%s" , a.name);
for (i = 0; i < 11; i++)
scanf ("%d" , &a.phone[i]);
scanf ("%s" , a.relation );
}
fclose (p);
FILE * fp = fopen ("person.txt" , "rb+");
if (fp == 0)
return 0;
p2 = &b;
while (!feof (fp))
{
fread (p2 , sizeof (struct person) , 1 , fp);
printf ("%s " , p2->name);
for (i = 0; i < 10; i++)
printf ("%d" , p2->phone[i]);
printf ("%d " , p2->phone[i]);
printf ("%s" , p2->relation);
printf ("\n");
}
fclose (fp);
return 0;
}
为什么最后的那个数据总会输出2次啊