用txt编程c语言后怎么运行,编写一个程序将-C语言编程,我写了一个程序,但不能运行.首先把a.txt和b.t 爱问知识人...

本文介绍了一种使用链表进行文本文件比较的方法。通过将两个文本文件的内容读入链表,然后遍历链表来查找重复的数据项,并将这些重复项写入第三个文件中。该方法展示了链表的基本操作及文件读写的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先把a.txt和b.txt读入链表,然后判断是否有相同,如果没有相同的就输到C.txt中。

#include

#include

#include

typedef struct

首先把a.txt和b.txt读入链表,然后判断是否有相同,如果没有相同的就输到C.txt中。

#include

#include

#include

typedef struct a{

int aa;

int ab;

struct a* next;}a;

typedef struct b{

int ba;

int bb;

struct b* next;}b;

typedef struct c{

int ca;

int cb;

struct c* next;}c;

int main(){

FILE* fp;

char x;

int along,blong,clong;

a *ahead,*ap,*aq;

b *bhead,*bp,*bq;

c *chead,*cp,*cq;

along=0;

blong=0;

clong=0;

ahead=(a*)malloc(sizeof(a));

ahead->next=NULL;

aq=ahead;

if((fp=fopen("a.txt","r"))==NULL){printf("文件a读取失败!!\n");return -1;}

while(1)

{

ap=(a*)malloc(sizeof(a));

ap->next=NULL;

fscanf(fp,"%d",&ap->aa);fscanf(fp,"%c",&x); fscanf(fp,"%d",&ap->ab);fscanf(fp,"%c",&x);

aq->next=ap;

aq=ap;

along++;

if(x!='\n')break; }

fclose(fp);

bhead=(b*)malloc(sizeof(b));

bhead->next=NULL;

bq=bhead;

if((fp=fopen("b.txt","r"))==NULL){printf("文件b读取失败!!\n");return -2;}

while(1)

{

bp=(b*)malloc(sizeof(b));

bp->next=NULL;

fscanf(fp,"%d",&bp->ba);

fscanf(fp,"%c",&x);

fscanf(fp,"%d",&bp->bb);

fscanf(fp,"%c",&x);

bq->next=bp;

bq=bp; blong++;

if(x!='\n')break; }

fclose(fp);

ap=ahead;

bp=bhead;

chead=(c*)malloc(sizeof(c));

chead->next=NULL;

cq=chead;

ap=ap->next;

bp=bp->next;

while(ap != NULL) { bp = bhead; while (bp != NULL) { if(ap->aa==bp->ba&&ap->ab==bp->bb) { cp=(c*)malloc(sizeof(c));

cp->next=NULL;

cp->ca=bp->ba;

cp->cb=bp->bb;

cq->next=cp;

cq=cp;

clong++; }

bp = bp->next; }

ap = ap->next; }

if((fp=fopen("c.txt","w"))==NULL){printf("文件c建立失败!!\n");return -3;}

cp=chead;

while(1) {

cp=cp->next;

fprintf(fp,"%d\t",cp->ca);

fprintf(fp,"%d\t",cp->cb);

if(cp->next==NULL)break;

else fprintf(fp,"\n"); }

fclose(fp);

return 0;

}

展开

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值