代码如下:
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
#define n 2
struct book //建立结构体数组
{
char zhanghao[8];
char num[11];
char suoyinhao[6];
char time[10];
char returntime[10];
}a[n],b[n],c[n];
struct node //建立结构体链表
{
char zhanghao[8];
char num[11];
char suoyinhao[6];
char time[10];
char returntime[10];
struct node *next;
};
FILE *fp1,*fp2,*fp3;
struct node *insertone(struct node *head1) /*输入a.txt中所有的信息,并构建新的带头结点的链表*/
{
int i;
struct node *p,*q;
head1=q=(struct node *)malloc(sizeof(struct node));
printf(" ~~~~~~欢迎进入借还书信息系统,请输入相关信息~~~~~~\n");
for(i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("账号:");
scanf("%s",a[i].zhanghao);
printf("学号:");
scanf("%s",a[i].num);
printf("图书索引号:");
scanf("%s",a[i].suoyinhao);
printf("借书时间:");
scanf("%s",a[i].time);
printf("\n");
q->next=p;
q=p;
}
p->next=0;
return head1;
}
struct node *inserttwo(struct node *head2) /*输入b.txt中所有的信息,并构建新的带头结点的链表*/
{
int i;
struct node *p,*q;
head2=q=(struct node *)malloc(sizeof(struct node));
for(i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("账号:");
scanf("%s",b[i].zhanghao);
printf("学号:");
scanf("%s",b[i].num);
printf("图书索引号:");
scanf("%s",b[i].suoyinhao);
printf("还书时间:");
scanf("%s",b[i].returntime);
printf("\n");
q->next=p;
q=p;
}
p->next=0;
return head2;
}
void showone(struct node *head1) /*保存到a.txt文件中并显示出来*/
{
int i;
struct node *temp;
temp=head1;
printf("a.txt\n");
fp1=fopen("a.txt","w");
for(i=0;i<n;i++)
if(fwrite(&a[i],sizeof(struct book),1,fp1)!=1) /*将结构体数组a[i]中的数据写入a.txt文件*/
printf("文件写入数据错误\n");
fclose(fp1);
fp1=fopen("a.txt","r");
printf("\n账号 学号 图书索引号 借书时间\n");
for(i=0;i<n;i++)
{
fread(&a[i],sizeof(struct book),1,fp1); /*从a.txt文件中读出数据并打印出来*/
printf("%s %11s %8s %15s\n",a[i].zhanghao,a[i].num,a[i].suoyinhao,a[i].time);
temp=temp->next;
}
fclose(fp1);
}
void showtwo(struct node *head2) /*保存到b.txt文件中并显示出来*/
{
int i;
struct node *temp;
temp=head2;
printf("b.txt\n");
fp2=fopen("b.txt","w");
for(i=0;i<n;i++)
if(fwrite(&b[i],sizeof(struct book),1,fp2)!=1) /*将结构体数组b[i]中的数据写入b.txt文件*/
printf("文件写入数据错误\n");
fclose(fp2);
fp2=fopen("b.txt","r");
printf("\n账号 学号 图书索引号 还书时间\n");
for(i=0;i<n;i++)
{
fread(&b[i],sizeof(struct book),1,fp2); /*从a.txt文件中读出数据并打印出来*/
printf("%s %11s %8s %15s\n",b[i].zhanghao,b[i].num,b[i].suoyinhao,b[i].returntime);
temp=temp->next;
}
fclose(fp2);
}
struct node *loadone(void) /*将文件a中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
int i;
struct node *head1, *p,*q;
struct book a[n];
head1=(struct node *)malloc(sizeof(struct node));
q=head1=NULL;
if((fp1=fopen("a.txt","rb"))==NULL)
return head1;
else
{while(!feof(fp1))
{
for(i=0;i<n;i++)
if(fread(&a[i],sizeof(struct book),1,fp1)==1)
{p=(struct node *)malloc(sizeof(struct node));
strcpy(p->zhanghao,a[i].zhanghao);
strcpy(p->num,a[i].num);
strcpy(p->suoyinhao,a[i].suoyinhao);
strcpy(p->time,a[i].time);
head1=p;
p->next=q;
q=head1;
}
}
fclose(fp1);
return head1;
}
}
struct node *loadtwo(void) /*将文件b中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
int i;
struct node *head2, *p,*q;
struct book b[n];
head2=(struct node *)malloc(sizeof(struct node));
q=head2=NULL;
if((fp2=fopen("b.txt","rb"))==NULL)
return head2;
else
{while(!feof(fp2))
{
for(i=0;i<n;i++)
if(fread(&b[i],sizeof(struct book),1,fp2)==1)
{p=(struct node *)malloc(sizeof(struct node));
strcpy(p->zhanghao,b[i].zhanghao);
strcpy(p->num,b[i].num);
strcpy(p->suoyinhao,b[i].suoyinhao);
strcpy(p->returntime,b[i].returntime);
head2=p;
p->next=q;
q=head2;
}
}
fclose(fp2);
return head2;
}
}
void hebing(struct node *head1, struct node *head2) /*合并两个文件*/
{
char ch;
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("a.txt","r"))==NULL)
{printf("打开文件a.txt失败!\n");
exit(1);
}
else
printf("打开文件a.txt成功!\n");
if((fp2=fopen("b.txt","r"))==NULL)
{printf("打开文件b.txt失败!\n");
exit(2);
}
else
printf("打开文件b.txt成功!\n");
if((fp3=fopen("c.txt","r"))==NULL)
{printf("打开文件c.txt失败!\n");
exit(3);
}
else
printf("打开文件c.txt成功!\n");
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
struct node *loadthree(void) /*将文件c中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
int i;
struct node *head, *p,*q;
struct book c[n];
head=(struct node *)malloc(sizeof(struct node));
q=head=NULL;
if((fp3=fopen("c.txt[]","rb"))==NULL)
return head;
else
{while(!feof(fp3))
{
for(i=0;i<n;i++)
if(fread(&c[i],sizeof(struct book),1,fp3)==1)
{p=(struct node *)malloc(sizeof(struct node));
strcpy(c[i].zhanghao,p->zhanghao);
strcpy(c[i].num,p->num);
strcpy(c[i].suoyinhao,p->suoyinhao);
strcpy(c[i].time,p->time);
strcpy(c[i].returntime,p->returntime);
head=p;
p->next=q;
q=head;
}
}
fclose(fp3);
return head;
}
}
void showthree(struct node *head) /*保存c.txt文件中的信息并显示出来*/
{
int i;
struct node *p;
p=head;
printf("c.txt\n");
fp3=fopen("c.txt","w");
for(i=0;i<n;i++)
if(fwrite(&c[i],sizeof(struct book),1,fp3)!=1)
printf("文件写入数据错误\n");
fclose(fp3);
fp3=fopen("c.txt","r");
printf("\n账号 学号 图书索引号 借书时间 还书时间\n");
for(i=0;i<n;i++)
{
fread(&c[i],sizeof(struct book),1,fp3);
printf("%s %11s %8s %15s %15s\n",c[i].zhanghao,c[i].num,c[i].suoyinhao,c[i].time,c[i].returntime);
p=p->next;
}
fclose(fp3);
}
main()
{
struct node *head;
struct node *head1;
struct node *head2;
head1=insertone(head1); //输入a.txt文件中的信息
head2=inserttwo(head2); //输入b.txt文件中的信息
showone(head1); //保存a.txt文件中的信息并打印出来
showtwo(head2); //保存b.txt文件中的信息并打印出来
head1=loadone(); //从a.txt中读取相关信息存储到链表中
head2=loadtwo(); //从b.txt中读取相关信息存储到链表中
hebing(head1,head2); //合并两个文件
head=loadthree(); //对合并后的文件建立链表
showthree(head); //保存链表中的信息到文件中并打印出来
}
a文件和b文件合并后的c文件内容一直不显示呢?