| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 469 人关注过本帖
标题:为什么在文件里存好的内容在经过这个函数之后就变得乱七八糟了呢?
只看楼主 加入收藏
千树
Rank: 1
等 级:新手上路
帖 子:62
专家分:1
注 册:2013-11-4
结帖率:100%
收藏
已结贴  问题点数:18 回复次数:4 
为什么在文件里存好的内容在经过这个函数之后就变得乱七八糟了呢?
void amend()     //学生信息修改
{
    FILE *fp;
    char a[20];
    struct Student *p1,*p2,*p,*head;
    struct Student stu;
    int n,i,j,m=0;
    if((fp=fopen("student_list.txt","r"))==NULL)
    {
        printf("cannot open file");
        exit(0);
    }
    printf("Please enter the student's ID:");  
    scanf("%d",&n);
    printf("Which one do you want to amend? Please choose the number:\n");
    printf("1:name   2:sex   3:ID   4:academy    5:clas\n");
    scanf("%d",&i);
    head=NULL;
    p1=p2=(struct Student*)malloc(sizeof(struct Student));   //开辟新单元
    while(!feof(fp))     //如果没有遇见文件的结束标志
    {
        fscanf(fp,"%s",&p1->name);   //从磁盘文件读入
        fscanf(fp,"%s",&p1->sex);
        fscanf(fp,"%d",&p1->ID);
        fscanf(fp,"%s",&p1->academy);
        fscanf(fp,"%s",&p1->clas);
        m++;                 
        if(m==1)head=p1;   
        else p2->next=p1;
        p2=p1;
        p1=(struct Student*)malloc(sizeof(struct Student));      
    }
    p2->next=NULL;
    free(p1);
    fclose(fp);
    p=head;
    while(p!=NULL)
    {
        if(n==p->ID)
        switch(i)
        {
            case 1:
                printf("Please enter the new name:");
                scanf("%s",a);  
                strcpy(p->name,a);   //利用复制函数来修改姓名
                break;
            case 2:
                printf("please enter the new sex:");
                scanf("%s",a);
                strcpy(p->sex,a);
                break;
            case 3:
                printf("Please enter the new ID:");
                scanf("%d",&j);
                p->ID=j;
                break;
            case 4:
                printf("Please enter the new academy:");
                scanf("%s",a);
                strcpy(p->academy,a);
                break;
            case 5:printf("Please enter the new clas:");
                scanf("%s",a);
                strcpy(p->clas,a);
                break;
        }
        p=p->next;
    }
    fp=fopen("student_list.txt","w");
    p=head;
    while(p!=NULL)
    {
        fprintf(fp,"%s\n%s\n%d\n%s\n%s\n",stu.name,stu.sex,stu.ID,stu.academy,stu.clas);
        p=p->next;
    }
    fclose(fp);
}
 

原来的文件内容结果变成了这样的:
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
-858993460
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
烫烫烫烫烫烫烫烫烫烫烫烫烫8
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
-858993460
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫8
烫烫烫烫烫烫烫烫烫烫烫烫烫8
搜索更多相关主题的帖子: number cannot file 信息 
2013-12-21 23:32
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:8 
通常这种情况是因为输出地址或字串没有结束标志

三十年河东,三十年河西,莫欺少年穷!
2013-12-21 23:45
千树
Rank: 1
等 级:新手上路
帖 子:62
专家分:1
注 册:2013-11-4
收藏
得分:0 
回复 2楼 韶志
p2->next=NULL;
这步不是已经让链表尾为空了吗?
2013-12-21 23:48
神机军师
Rank: 7Rank: 7Rank: 7
来 自:游鱼潜水
等 级:黑侠
威 望:2
帖 子:202
专家分:542
注 册:2013-12-21
收藏
得分:10 
说明一下,我是新手。。。。。
1.没有struct的原型 我看看咯
fscanf(fp,"%s",&p1->name);   //从磁盘文件读入
fscanf(fp,"%s",&p1->sex);
fscanf(fp,"%d",&p1->ID);
fscanf(fp,"%s",&p1->academy);
fscanf(fp,"%s",&p1->clas);

这里,如果 name sex academy clas 是字符串模式 那么 后面的格式不应该加 & 吧。。。

2.最后那里
    while(p!=NULL)
    {
        fprintf(fp,"%s\n%s\n%d\n%s\n%s\n",stu.name,stu.sex,stu.ID,stu.academy,stu.clas);
        p=p->next;
    }

你的指针变量是p...  这里怎么是stu。。。

没有用软件调试,看到了这些 不知道是不是问题所在。。 希望对你有帮助. 大晚上 冻死了

未知令人期待!
2013-12-22 00:20
千树
Rank: 1
等 级:新手上路
帖 子:62
专家分:1
注 册:2013-11-4
收藏
得分:0 
回复 4楼 神机军师
oh,yes!非常感谢!
2013-12-22 10:37
快速回复:为什么在文件里存好的内容在经过这个函数之后就变得乱七八糟了呢?
数据加载中...
 
   



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

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