C语言----Xcode上读入文件时总会夹着一个我写入时没有写入的0,而且出现在中间,求助大佬
int main()
{
int i=999,j,k=0;
FILE *fp;
if ((fp = fopen(“stu.txt”,“rw”)) == NULL)
{
printf(“error\n”);
exit(0);
}
student *head = NULL;
student *first = NULL;
printf("Do you want to get the previous data or create the new data:\n");
printf("1.The previous data\n");
printf("2.The new data\n");
printf("You choose No.");
scanf("%d",&j);
switch (j)
{
case 1:
head = (student *)malloc(sizeof(student));
for(;head!=NULL;head=head->next)
{
head->next = (student *)malloc(sizeof(student));
if (k==0)
{
first = head;
}
k++;
fread(head,sizeof(struct stu),1,fp);
show(head);
printf("\n");
}
head = first;
break;
case 2:printf("------------Create the students' List-----------\n");
head = create(head);
first = head;
show(head);
if ((fp = fopen("stu.txt","wt")) == NULL)
{
printf("error\n");
exit(0);
}
for (;head!=NULL;head = head->next)
{
fwrite(head,sizeof(struct stu),1,fp);
}
fclose(fp);
break;
default: printf("You choose the wrong number!\n");
break;
}
while(i!=0)
{
printf("Do you want to change some thing or exit?\n");
printf("1.change some data from student\n");
printf("2.No.Exit\n");
printf("You choose No.");
scanf("%d",&i);
switch (i)
{
case 1:cha(head);
break;
case 2:i=0;
break;
}
break;
}
printf("------------The new students' List-----------\n");
printf("3.%o\n",first);
show(first);
fclose(fp);
}