C语言 两个txt文件操作小程序 求代码!!100分
答案:3 悬赏:0 手机版
解决时间 2021-01-22 02:37
- 提问者网友:轻浮
- 2021-01-21 14:19
C语言 两个txt文件操作小程序 求代码!!100分
最佳答案
- 五星知识达人网友:躲不过心动
- 2021-01-21 14:43
#include
#include
#include
void main()
{
FILE *fpa,*fpb,*fpc;
char k[300]={0},kk[300]={0}; //k是b里的字符串,kk是a里的字符串
fpa=fopen("a.txt","r");
fpb=fopen("b.txt","r");
fpc=fopen("c.txt","w"); //先把内容存在文件c里,再转到a里面
if(fpa==NULL||fpb==NULL)
{
printf("打开文件失败\n");
printf("请确认你是否已新建好\"a.txt\"和\"b.txt\"\n");
return;
}
fscanf(fpb,"%s",k);
fscanf(fpa,"%s",kk);
while(kk[0]!='\0') //开始删除字符串
{
if(strcmp(kk,k)!=0)
{
fprintf(fpc,"%s\n",kk);
}
memset(kk,0,sizeof(kk));
fscanf(fpa,"%s",kk);
}
fclose(fpa);
fclose(fpb);
fclose(fpc);
fpc=fopen("c.txt","r");
fpa=fopen("a.txt","w");
memset(kk,0,sizeof(kk));
fscanf(fpc,"%s",kk);
while(kk[0]!='\0') //开始转到a里面
{
fprintf(fpa,"%s\n",kk);
memset(kk,0,sizeof(kk));
fscanf(fpc,"%s",kk);
}
fclose(fpc);
system("del c.txt"); //把新建的c.txt删掉
printf("已完成\n");
}
#include
#include
void main()
{
FILE *fpa,*fpb,*fpc;
char k[300]={0},kk[300]={0}; //k是b里的字符串,kk是a里的字符串
fpa=fopen("a.txt","r");
fpb=fopen("b.txt","r");
fpc=fopen("c.txt","w"); //先把内容存在文件c里,再转到a里面
if(fpa==NULL||fpb==NULL)
{
printf("打开文件失败\n");
printf("请确认你是否已新建好\"a.txt\"和\"b.txt\"\n");
return;
}
fscanf(fpb,"%s",k);
fscanf(fpa,"%s",kk);
while(kk[0]!='\0') //开始删除字符串
{
if(strcmp(kk,k)!=0)
{
fprintf(fpc,"%s\n",kk);
}
memset(kk,0,sizeof(kk));
fscanf(fpa,"%s",kk);
}
fclose(fpa);
fclose(fpb);
fclose(fpc);
fpc=fopen("c.txt","r");
fpa=fopen("a.txt","w");
memset(kk,0,sizeof(kk));
fscanf(fpc,"%s",kk);
while(kk[0]!='\0') //开始转到a里面
{
fprintf(fpa,"%s\n",kk);
memset(kk,0,sizeof(kk));
fscanf(fpc,"%s",kk);
}
fclose(fpc);
system("del c.txt"); //把新建的c.txt删掉
printf("已完成\n");
}
全部回答
- 1楼网友:未来江山和你
- 2021-01-21 15:36
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MaxR 300
#define MaxL 100
char stra[MaxR][MaxL],strb[MaxR][MaxL];
int atrnum[MaxR]={0};
void main()
{
FILE *fp;
int i,j,k,a=0,b=0;
if((fp=fopen("a.txt","rt"))==NULL)
{
printf("cannot open file\n");
return;
}
while (fgets(stra[a],100,fp))
{
a++;
if(a>=MaxR)break;
}
fclose(fp);
if((fp=fopen("b.txt","rt"))==NULL)
{
printf("cannot open file\n");
return;
}
while (fgets(strb[b],100,fp))
{
b++;
if(b>=MaxR)break;
}
fclose(fp);
for(i=0;i for(j=0;j {
if(strcmp(stra[i],strb[j])==0)
{
atrnum[i]=1;
break;
}
}
if((fp=fopen("a.txt","wt"))==NULL)
{
printf("cannot open file\b");
return;
}
for(i=0;i if(atrnum[i]==0)fprintf(fp,"%s",stra[i]);
fclose(fp);
}
- 2楼网友:拾荒鲤
- 2021-01-21 15:30
你这个说实在的还真不简单,我尽量给你弄出来,我调试好后发你邮箱!追问恩恩 好的 如果你能做出来 我再增加100分悬赏 一次性给你250分,敬候佳音追答呵呵你这个分不太好吧。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯