| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2594 人关注过本帖
标题:为什么用fprintf后文件还是空的
取消只看楼主 加入收藏
星行星际
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2016-1-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
为什么用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+也不行
搜索更多相关主题的帖子: include 电话 姓名 
2016-04-09 12:11
星行星际
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2016-1-6
收藏
得分:0 
解决了,是因为if(fp=fopen("data-1.txt","a+")==NULL),
应该写成
fp=fopen("data-1.txt","a+")
if(fp==NULL)
这样分开写,不然系统会出错
2016-04-09 12:48
快速回复:为什么用fprintf后文件还是空的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011399 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved