| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 639 人关注过本帖
标题:碰到一个棘手的问题。求大神赐教
只看楼主 加入收藏
菜鸟0
Rank: 2
等 级:论坛游民
帖 子:37
专家分:91
注 册:2012-6-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
碰到一个棘手的问题。求大神赐教
我要保存一个文件。是关于结构体,链表和文件的。我的程序开始时是直接将数据以2进制方式写入文件,然后由文件生成链表。再对链表进行操作就很方便了。后来遇到了问题,我对链表数据的修改不会引起我的文件的改变,相当于我没法对我的文件数据进行修改了。我尝试先修改在用一个保存函数重新保存,还是不行,因为我的链表是基于原文件生成的。但我又必须对原文件进行数据的修改。求解一个方法。这是程序
#include<stdio.h>                                       
#include<stdlib.h>                                      
#include<string.h>
#include<windows.h>                              
#include<time.h>
int n=0;
void input()                                    输入数据函数,
{printf("录入会丢失全部存入的信息,是否继续(y/n)");
 char x;
scanf("%c",&x);
getchar();
if(x=='y'||x=='Y')
{
  struct employees                                      定义结构体
  {
       int number;
       char name[20];
       char sex;
       char danwei[50];
       char address[50];
       int phone;
       float jiben;
       float jintie;
       float butie;
       float yingfa;
       float dianhua;
       float shuidian;
       float fangzu;
       float suodeshui;
       float weisheng;
       float gongjijin;
       float hejikoukuan;
       float shifagongzi;
       };
{
      struct employees teacher;                                                
      char numstr[20],ch;
      float x;
      FILE *fp;
      if((fp=fopen("teacher.dat","wb"))==NULL)                 直接将数据写入文件
      {printf("can't open the file teacher.dat\n");
      exit(1);
      }
      do
      {   printf("number:");gets(numstr);teacher.number=atoi(numstr);
          printf("name:");gets(teacher.name);
          printf("sex:");teacher.sex=getchar();getchar();
          printf("danwei:");gets(teacher.danwei);
          printf("address:");gets(teacher.address);
          printf("phone:");gets(numstr);teacher.phone=atoi(numstr);
          printf("jiben:");gets(numstr);teacher.jiben=atof(numstr);
          printf("butie:");gets(numstr);teacher.butie=atof(numstr);
          printf("yingfa:");gets(numstr);teacher.yingfa=atof(numstr);
          printf("dianhuafei:");gets(numstr);teacher.dianhua=atof(numstr);
          printf("shuidianfei:");gets(numstr);teacher.shuidian=atof(numstr);
          printf("fangzu:");gets(numstr);teacher.fangzu=atof(numstr);
          printf("suodeshui:");gets(numstr);teacher.suodeshui=atof(numstr);
          printf("weishengfei:");gets(numstr);teacher.weisheng=atof(numstr);
          printf("gongjijin:");gets(numstr);teacher.gongjijin=atof(numstr);
          printf("hejikoukuan:");gets(numstr);teacher.hejikoukuan=atof(numstr);
          printf("shifagongzi:");gets(numstr);teacher.shifagongzi=atof(numstr);
          n++;
          fwrite(&teacher,sizeof(struct employees),1,fp);
          printf("have another employ record(y/n)?");
          ch=getchar();getchar();
          }while(ch=='y'||ch=='Y');
          fclose(fp);
          printf("输入了%d个员工",n);
          }
}

else
exit(0);}                         输入数据文件结束
        
struct employees2        定义一个新的结构体,比上面那个多一个指向结构体的指针
  {
       int number;
       char name[20];
       char sex;
       char danwei[50];
       char address[50];
       int phone;
       float jiben;
       float jintie;
       float butie;
       float yingfa;
       float dianhua;
       float shuidian;
       float fangzu;
       float suodeshui;
       float weisheng;
       float gongjijin;
       float hejikoukuan;
       float shifagongzi;
       struct employees2 *link;
       };
      struct employees2 *load()               将写入文件的数据生成链表的函数
       {
              struct employees2 *head,*p,*q;
              FILE *fp;
              if((fp=fopen("teacher.dat","rb"))==NULL)
              {printf("can't open file \n");
              getchar();
              }
              head=NULL;
              q=(struct employees2 *)malloc(sizeof(struct employees2));
              while(fread(q,sizeof(struct employees2)-4,1,fp)==1)
              {q->link=NULL;
               if(head==NULL)
                 {head=q;
                  p=head;
                  }
               else
                  {
                  p->link=q;
                  p=p->link;
                  }
              q=(struct employees2 *)malloc(sizeof(struct employees2));
              }
              free(q);
              fclose(fp);
              return(head);
              }  
 void display ()                  显示结果的函数
 {
      struct employees2 *load();
      void print(struct employees2 *p);
      struct employees2 *p;
      p=load();
      while(p!=NULL)
      {print(p);
      p=p->link;}
 }
 void print(struct employees2 *p)
  {   printf("number:%d\nname:%s\nsex:%c\n",p->number,p->name,p->sex);
      printf("danwei:%s\naddress:%s\nphone:%d\n",p->danwei,p->address,p->phone);
      printf("jiben:%f\njintie:%f\nbutie:%f\nyingfa:%f\n",p->jiben,p->jintie,p->butie,p->yingfa);
      printf("dianhua:%f\nshuidian:%f\nfangzu:%f\nsuodeshui:%f\n",p->dianhua,p->shuidian,p->fangzu,p->suodeshui);
      printf("weisheng:%f\ngongjijing:%f\n",p->weisheng,p->gongjijin);
      printf("hejikoukuan:%f\nshifagongzi:%f\n",p->hejikoukuan,p->shifagongzi);
      
 }
 
 void search()                     查询函数
 {   
      
      struct employees2 *load();
      struct employees2 *p;
      p=load();
      void print (struct employees2 *p);
      char x[20];
      int y,z,n=0;
      p=load();
      printf("1:按姓名查找\t2:请选择按工号查找\n");
      scanf("%d",&z);
      switch(z)
 {     case 1:
      {
              printf("请输入姓名:按enter结束\n");
              scanf("%s",&x);
              while(p!=NULL)
                {
                     if(strcmp(p->name,x)==0)
                     {print(p);
                     n++;}
                     p=p->link;
                }
             if(n==0)
             printf("该姓名不存在\n");
             break;
       }
       case 2:
       {
            printf("请输入工号,按enter结束\n");
            scanf("%d",&y);
            while(p!=NULL)
            {
                          if(p->number==y)
                          {print(p);
                          n++;
                          }
                          p=p->link;
             }
             if(n==0)
             printf("该工号职员不存在\n");
             break;}
      
        default: printf("输入有误");
    }
        
  }                  

                  
       main()             主函数
  {
        void input();
        void search();
        struct employees2 *load();
        struct employees2 *load();
        void display();
        struct employees2 *p;
        input();
        display();
        p=load();
        p->number=10;         在这里对结构体进行更改,改过之后没有影响到原文件,求一个方法使得对结构体的更改可以让原文件也做相应的改变。   
        system("pause");
        
        }
                        
      
         
         
      
      
      
      
      
      
      
      
      
      
      
      
      
      
程序。
搜索更多相关主题的帖子: 数据 include 结构体 
2012-06-21 21:36
ln6265431
Rank: 5Rank: 5
等 级:职业侠客
帖 子:66
专家分:325
注 册:2012-3-29
收藏
得分:10 
读取以后在链表中修改,再用写入函数“wb”写入不就行了吗
2012-06-22 13:56
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
对使用的概念不了解,整个设计就是错误的。链表用在文件上的干什么?文件的本质是什么,看来也是不清楚的了。

[ 本帖最后由 TonyDeng 于 2012-6-22 14:01 编辑 ]

授人以渔,不授人以鱼。
2012-06-22 13:58
菜鸟0
Rank: 2
等 级:论坛游民
帖 子:37
专家分:91
注 册:2012-6-20
收藏
得分:0 
错误都不在此。。这个设计也不是错的。。。
自己解决了。。。不过同样谢谢了。
2012-06-25 18:24
快速回复:碰到一个棘手的问题。求大神赐教
数据加载中...
 
   



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

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