碰到一个棘手的问题。求大神赐教
我要保存一个文件。是关于结构体,链表和文件的。我的程序开始时是直接将数据以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");
}
程序。