文件管理 · 2022年7月25日

西工大c语言文件比较|C语言 对比两个文件内容的不同

1. C语言 对比两个文件内容的不同

如果两个抄文件长度不同。比如头一个文件内容为: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;}

2. 【求助】C语言编程,比较两个文件相似度!

#include"stdio.h" #include"string.h" #define N(a,b) 2.0*a/b*100 FILE *pa,*pb; int t,m; char a[100],b[100]; /*创建文件1*/ void Creat1_Z() { if((pa=fopen("d:\\file1.txt","w+"))==NULL) printf("\tCannot open this file!\n"); else { printf("\tEnter a string to File1:\n"); getchar(); gets(a); fputs(a,pa); printf("\tSuccess!\n"); } fclose(pa); } /*创建文件2*/ void Creat2_Z() { if((pb=fopen("d:\\file2.txt","w+"))==NULL) printf("\tCannot open this file!\n"); else { printf("\tEnter a string to File2:\n"); getchar(); gets(b); fputs(b,pb); printf("\tSuccess!\n"); } fclose(pb); } /*相似性比较*/ void contrast( ) { char str1[15],str2[15]; int i=0,j=0,t=0,s,k=1,f=1; while(k==1) { for(s=0;(a[i]!=32)&&(a[i]!=0);s++,i++)//把==0去掉了 ,更简洁 str1[s]=a[i]; str1[s]='\0'; //if(str1=="") continue; f=1; j=0; while(f==1) { for(s=0;(b[j]!=32)&&(b[j]!=0);j++,s++) //把==0去掉了 ,更简洁 str2[s]=b[j]; str2[s]='\0'; //if(str2=="") continue; if(strcmp(str1,str2)==0) { t++; f=0; continue; } j++; if(b[j]==0) f=0; }i++; if(a[i]==0) k=0; } printf("相同单词数:%d\n",t); for(i=0,j=0,s=0;a[i]!=0;i++) if(a[i]==32) s=0; else if(s==0) {s=1;j++;} for(i=0,s=0;b[i]!=0;i++) if(b[i]==32) s=0; else if(s==0) {s=1;j++;} printf("相似度:%5.2f%%\n",N(t,j)); if(N(t,j)>=80) printf("相同!\n"); else printf("不同!\n"); } /**/ void mune() { printf("*****************判断两个文件是否相同*****************\n"); printf("\t\t 1:创建文件1\n"); printf("\t\t 2:创建文件2\n"); printf("\t\t 3:判断相似性\n"); printf("\t\t 4:退出\n"); printf("####################请选择###########################\n"); printf("****************************************************\n"); } /**/ void main() { while(t!=4) { mune(); //单词拼错了,应该是menu,菜单的意思scanf("%d",&t); switch (t){ case 1: Creat1_Z(); break; case 2: Creat2_Z(); break; case 3: contrast();break; case 4:break;default: printf("输入错误!请重新输入!\n");} } }我没有找到你说的个goto语句哦,不过我把那一大堆if else改掉了,而且在vc++环境下运行的很好~

3. c语言比较两个文件不同项

先读取第一个文件, 将所有人的名单存在一个字符串数组中。

然后读另一个文件,到第一个数组中去查找,如果文件没错的话,那么每个都应该找到。

删掉找到的。

最后输出剩下的。

4. 100分 c语言对比两个文件

//奇怪啊,我试了是可以的啊~//非常简单的程序,对比a.dat和b.dat是否相同#include<stdio.h>void main(){ FILE *fp1,*fp2; fp1 = fopen("a.dat","rb"); fp2 = fopen("b.dat","rb"); int n=0; unsigned a,b; while( (!feof(fp1)) && (!feof(fp2)) ) { fread(&a,1,1,fp1); fread(&b,1,1,fp2); if(a != b) { n=1; break; } } if(n==1) printf("no same!\n"); else printf("same!\n"); fclose(fp1); fclose(fp2);}

5. C语言文件比较

check the two files

6. C语言比较两个txt文件

使用a[20]存储a文件中的20个正确答案使用b[10000]存储b文件中的10000个检测结果使用文件操作将数据读出来并将两个数组初始化(这一步没问题吧)使用num[20]记录b中对应a的每种情况的个数使用c[10000]记录b中每种结果是否在a中有对应使用一个嵌套循环,将10000个结果中的每个结果和20中情况相比对如果属于20种情况中的,相应的num就加一,对应的c标识为正确,已找出如果不属于这20中情况,对应的c标识为错误,未找出。哥们,这个说得够详细了吧,编代码也就分分钟的了

7. 重赏C语言“文件中比较大小的问题”

就是循环读文件——查找——做数据处理吧

8. C语言指针读取文件内容比对

基本的,你的代码有语法错误 printf(“登录失败”);这里的符号不是英文符号

文本中保存的是两个字符串(空格隔开),你是想让第一个作为帐号,第二个为密码吗

fgets你指定了大小100,而且没有那么多字符能读取,所以它把整个文件中的所有字符都读取了,包含空格,gets()也一样。

所以你输入帐号加密码,中间用空格隔开才是正确的输入方式

然后我试了你的代码,得到了正确的结果,文本中保存的123456 789//注意如果后面有回车换行也可能造成你的错误

9. 用C语言编一个文件对比的程序

#include<stdio.h>#include<string.h>#define SIZE 12#define LINE 50000int main(){ FILE *from0,*from1; char str0[SIZE],str1[SIZE]; char file1[256],file2[256]; int i,j; printf("Input the name of file1:\n"); gets(file1); printf("Input the name of file2:\n"); gets(file2); if((from0=fopen(file1,"r"))==NULL) { printf("Can't open file1 :%s\n",file1); getch(); return 0; } if((from1=fopen(file2,"r"))==NULL) { printf("Can't open file2 :%s\n",file2); fclose(from0); getch(); return 0; } for(i=LINE;i!=0;i–) { fscanf(from0,"%s",str0); fseek(from1,0,SEEK_SET); for(j=LINE;j!=0;j–) { fscanf(from1,"%s",str1); if(!strcmp(str0,str1)) { printf("%s\n",str0); } } } fclose(from0); fclose(from1); return 1;}

10. C语言比较两个txt文件是否一样!

第一,你的第一个if语句里应该使用||而不是&&,第二,字符串比较不是直接比较的。要借助函数strcmp(str1,str2),如果一样就等于0