为什么用fprintf后文件还是空的
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char num[10];//学号
char sex[6];//性别
char name[10];//姓名
char phone[10];//电话号码
char qqnum[10];//QQ号码
char wechat[20];//微信号码
char home[20];//住址
struct student *next;
};
struct student *in(struct student *head)//建立链表,将文件中的初始数据导出到链表中存储
{
struct student*p,*tail;
FILE *fp;
int i,n=0,m;
p=(struct student *)malloc(sizeof(struct student));
if(!p)
exit(0);
p=head;
fp=fopen("data.txt","r");
if(fp==NULL)
exit(0);
while(n<10)
{
p=(struct student *)malloc(sizeof(struct student));
fscanf(fp,"%s%s%s%s%s%s%s",p->num,p->name,p->sex,p->phone,p->qqnum,p->wechat,p->home);
n++;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
//show(head);
return head;
fclose(fp);
}
void show(struct student *head)//显示全部通讯录
{
struct student *p=NULL;
p=head;
printf("-----------------------------------------------------------------\n");
printf("学号 姓名 性别 电话号码 QQ号码 微信号码 家庭住址\n");
while(p!=NULL)
{
printf("%s%10s%10s%10s%10s%10s%10s\n",p->num,p->name,p->sex,p->phone,p->qqnum,p->wechat,p->home);
p=p->next;
}
printf("------------------------------------------------------------------\n");
printf("输入0退出系统,输入1返回主菜单:");
int n;
scanf("%d",&n);
switch(n)
{
case 0:exit(0);break;
// case 1:menu();break;
}
}
int main()
{
FILE *fp;
struct student *p,*head;
head=in(head);
p=(struct student *)malloc(sizeof(struct student));
p=head;
if(fp=fopen("data-1.txt","a+")==NULL)
exit(0);
while(p!=NULL)
{
fprintf(fp,"%s%10s%10s%10s%10s%10s%12s\n",p->num,p->name,p->sex,p->phone,p->qqnum,p->wechat,p->home);
p=p->next;
}
fclose(fp);
}
in与show函数没有错误,就是用fprintf后文件还是空,若是用printf输出到屏幕,数据是正确的,将open中的w改成a+也不行