C语言比较两文件异同(txt)
用C语言比较两txt文件的同一行是否相同。例如:文件2的第一行和文件1的第一行不同,那么用文件1的第一行替代文件2的第一行。
各位懂得文件操作的发表下你们的高见吧!
求教了~~~
程序代码:#include<stdio.h>
#define Max 10000 //定义最大行数
#include<iostream>
#include<stdlib.h>
using namespace std;
int fseek(FILE *fp,long offset,int origin);
void main()
{
FILE *fpa ,* fpb ,* fpc;
fpa=fopen("aa.txt","r+") ;
fpb=fopen("bb.txt","r+") ;
fpc=fopen("cc.txt","w") ;
if( fpa==NULL )
{
printf("Can`t open A file\n");
exit(0);
}
if(fpb==NULL)
{
printf("Can`t open B file\n");
exit(0);
}
if( fpc==NULL )
{
printf("Can`t open C file\n");
exit(0);
}
char ch1[Max] ;
char ch2[Max] ;
int i(0);
int j(0);
while( !feof(fpa)){
fgets( ch1 ,Max ,fpa);
fgets( ch2 ,Max ,fpb);
if( strcmp(ch1,ch2)!=0 ){
fprintf( fpc,"%d\n",i);
fprintf( fpc,"%s\t%s\n",ch1,ch2);
j=fseek(fpb,0,0);
if(j>=0)
fputs(ch1,fpb);
else
return -1;
}
i++;
}
cout<<"总共比对"<<i<<"行"<<endl;
fclose(fpa);
fclose(fpb);
}
上面的自己写的代码 文件超过2行就出问题