C语言 对比两个文件内容的不同
要求:挑选两个txt文件,一个内含偶数(even.txt)一个内含奇数(oddd.txt),然后进行对比。运行之后应该是这样的:Enterfirstfilename:od...
要求:挑选两个txt文件,一个内含偶数(even.txt)一个内含奇数(oddd.txt),然后进行对比。
运行之后应该是这样的:
Enter first filename:odd.txt
Enter second filename:odd.txt
odd.txt and odd.txt are the same
或者
Enter first filename:odd.txt
Enter first filename:even.txt
odd.txt and even.txt are not the same
我的代码如下
#include<stdio.h>
#include<stdlib.h>
int main(){
char fname1[100],fname2[100];
int i,j,a[100],b[100];
FILE*fp1,*fp2;
printf("Enter first filename :");
scanf("%s",&fname1);
printf("Enter second filename :");
scanf("%s",&fname2);
fp1=fopen("fname1","r");
for(i=0;!feof(fp1);i++){
fscanf(fp1,"%d",&a[i]);
}
fclose(fp1);
fp2=fopen("fname2","r");
for(i=0;!feof(fp2);i++){
fscanf(fp2,"%d",&b[i]);
}
fclose(fp2);
if(a[i]!=b[i]){
printf(" %s and %s are not the same",fname1,fname2);
}
else{
printf(" %s and %s are the same",fname1,fname2);
}
getch();
} 展开
运行之后应该是这样的:
Enter first filename:odd.txt
Enter second filename:odd.txt
odd.txt and odd.txt are the same
或者
Enter first filename:odd.txt
Enter first filename:even.txt
odd.txt and even.txt are not the same
我的代码如下
#include<stdio.h>
#include<stdlib.h>
int main(){
char fname1[100],fname2[100];
int i,j,a[100],b[100];
FILE*fp1,*fp2;
printf("Enter first filename :");
scanf("%s",&fname1);
printf("Enter second filename :");
scanf("%s",&fname2);
fp1=fopen("fname1","r");
for(i=0;!feof(fp1);i++){
fscanf(fp1,"%d",&a[i]);
}
fclose(fp1);
fp2=fopen("fname2","r");
for(i=0;!feof(fp2);i++){
fscanf(fp2,"%d",&b[i]);
}
fclose(fp2);
if(a[i]!=b[i]){
printf(" %s and %s are not the same",fname1,fname2);
}
else{
printf(" %s and %s are the same",fname1,fname2);
}
getch();
} 展开
2个回答
展开全部
如果两个文件长度不同。比如头一个文件内容为:abcde第二个文件内容为:abcdefg会得出错误结果。
程序改写如下,已编译通过:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * f1 = fopen("dd.txt","r");/* 改为实际文件名 */
FILE * f2 = fopen("bb.txt","r");
char c1 = fgetc(f1);
char c2 = fgetc(f2);
while(!feof(f1) && !feof(f2)){
if(c1 != c2){printf("NO");system("pause");return 0;}
c1 = fgetc(f1);
c2 = fgetc(f2);
}
if(c1==EOF&&c2==EOF) /* 判断两个文件是否都到结尾 */
printf("YES");
else
printf("NO");
printf("\n");
fclose(f1);
fclose(f2);
system("pause");
return 0;
}
程序改写如下,已编译通过:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * f1 = fopen("dd.txt","r");/* 改为实际文件名 */
FILE * f2 = fopen("bb.txt","r");
char c1 = fgetc(f1);
char c2 = fgetc(f2);
while(!feof(f1) && !feof(f2)){
if(c1 != c2){printf("NO");system("pause");return 0;}
c1 = fgetc(f1);
c2 = fgetc(f2);
}
if(c1==EOF&&c2==EOF) /* 判断两个文件是否都到结尾 */
printf("YES");
else
printf("NO");
printf("\n");
fclose(f1);
fclose(f2);
system("pause");
return 0;
}
展开全部
#include<stdio.h>
#include<stdlib.h>
int main(){
char fname1[100],fname2[100];
int i,j,a[100],b[100],n=0;
FILE*fp1,*fp2;
printf("Enter first filename :");
scanf("%s",fname1); //
printf("Enter second filename :");
scanf(" %s",fname2); //
fp1=fopen("fname1","r");
for(i=0;!feof(fp1);i++){
fscanf(fp1,"%d",&a[i]);
}
fclose(fp1);
fp2=fopen("fname2","r");
for(i=0;!feof(fp2);i++){
fscanf(fp2,"%d",&b[i]);
}
fclose(fp2);
for(j=0;j<i;j++)
if(a[j]!=b[j]){n=1;
printf(" %s and %s are not the same",fname1,fname2);break;
}
if(n==0)
printf(" %s and %s are the same",fname1,fname2);
getch();
}
追问
好厉害!倒数两个for循环的作用是什么?
追答
for(j=0;j<i;j++)
if(a[j]!=b[j]) //如果有一个不同,则不同
{n=1; printf(" %s and %s are not the same",fname1,fname2);break; }
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询