user-0 提问: C语言文件比较问题

回复:
user: user-1
content: 引用 4 楼 zhangxiangDavaid 的回复:if (MAXLINES2 == 1280000) MAXLINES2 = 2500000; 这一句有特别的含义吗？ 被发现了。 当年调试到此时，发现我的破电脑2560000分配不了，2500000可以。

user: user-0
content: 引用 7 楼 u013163178 的回复:#include <stdio.h> #include <stdlib.h>  int file_size(FILE* fp) {     long mark = ftell(fp);     int size;     fseek(fp , 0 , SEEK_END);     size = ftell(fp);     fseek(fp , mark , SEEK_SET);     return size; } int main() {     FILE* f1 , *f2;     FILE* logs;     logs = fopen("log.txt" , "a+");     f1 = fopen("f1.txt" , "r");     f2 = fopen("f2.txt" , "r");      if(!logs)     {         fprintf(stderr , "can`t open log.txt\n");         exit(EXIT_FAILURE);     }     if(!f1)         {             fprintf(logs , "can`t open f1.txt\n");             exit(EXIT_FAILURE);         }     if(!f1)     {         fprintf(logs , "can`t open f2.txt");         exit(EXIT_FAILURE);     }      int a , b;     long n = 0;     size_t s1 , s2;     s1 = file_size(f1);     s2 = file_size(f2);     //比较文件的size     if(s1 != s2)         fprintf(logs,"file size not equel.\n");       //将文件的内容读到内存进行比较     while((a = fgetc(f1)) != EOF && (b = fgetc(f2)) != EOF)     {         n++;         if(a == b)             continue;         else         fprintf(logs, "%ldth byte not equel\n",n);     }       fclose(f1);     fclose(f2);     fclose(logs);     return 0; }  谢谢了，我之前想的是为用户输入的用户名和密码创建一个txt格式的文件，将其与存储好的文本进行比较，但是后来觉得有些多此一举，就想直接让输入的信心与文本进行比较，请问这样如何实现呢？

user: user-2
content: 1.将文本内容读入到内存到，数据在内存中什么形式存放，看你个人，hash，数组什么的都可以。 2.查找即可。

user: user-3
content: 对一楼的注释：  #define MAXCHARS 512 int MAXLINES = 10000, MAXLINES2; char *buf, *buf2; char PROG[256] = "PROG";//程序Program需要的文件列表 char LIST[256] = "LIST";//dir /b /s生成的实际文件列表List FILE *fp, *fl; int i, c, n, L, hh; int ignore_case = 0; char ln[MAXCHARS]; int icompare(const void *arg1, const void *arg2) { 	return stricmp((char *)arg1, (char *)arg2); } int compare(const void *arg1, const void *arg2) { 	return strcmp((char *)arg1, (char *)arg2); } int main(int argc, char **argv) { 	if (argc>1) strcpy(PROG, argv[1]);//命令行参数1覆盖PROG 	if (argc>2) strcpy(LIST, argv[2]);//命令行参数2覆盖LIST 	if (argc>3) ignore_case = 1;//若存在命令行参数3，忽略大小写 	if ((fl = fopen(LIST, "rt")) == NULL) { 		fprintf(stderr, "Can not open %s\n", LIST); 		fprintf(stderr, "Usage: %s [PROG] [LIST] [-i]\n", argv[0]); 		return 1; 	} 	if ((fp = fopen(PROG, "rt")) == NULL) { 		fclose(fl); 		fprintf(stderr, "Can not open %s\n", PROG); 		fprintf(stderr, "Usage: %s [PROG] [LIST] [-i]\n", argv[0]); 		return 2; 	} 	buf = (char *)malloc(MAXLINES*MAXCHARS); 	if (NULL == buf) { 		fclose(fl); 		fclose(fp); 		fprintf(stderr, "Can not malloc(%d LINES*%d CHARS)!\n", MAXLINES, MAXCHARS); 		return 4; 	} 	n = 0; 	hh = 0; 	i = 0;     //把LIST中的所有行读入到buf中 	while (1) { 		if (fgets(ln, MAXCHARS, fl) == NULL) break;// 		hh++; 		L = strlen(ln) - 1; 		if ('\n' != ln[L]) {////有限行最后一个字符是'\n',超长行忽略后面内容 			fprintf(stderr, "%s Line %d too long(>%d),spilth ignored.\n", LIST, hh, MAXCHARS); 			while (1) {//继续把超长行读完 				c = fgetc(fl); 				if ('\n' == c || EOF == c) break;//读到'\n'或文件结束符，即表明读完 			} 		} 		while (1) {//去掉行尾的'\n'和空格 			if ('\n' == ln[L] || ' ' == ln[L]) { 				ln[L] = 0; 				L--; 				if (L<0) break;//空行 			} 			else break;// 		} 		if (L >= 0) { 			strcpy(buf + i, ln); i += MAXCHARS; 			n++;             //若buf的空间不够，就扩充 			if (n >= MAXLINES) { 				MAXLINES2 = MAXLINES * 2; 				if (MAXLINES2 == 1280000) MAXLINES2 = 2500000; 				buf2 = (char *)realloc(buf, MAXLINES2*MAXCHARS); 				if (NULL == buf2) { 					free(buf); 					fclose(fl); 					fclose(fp); 					fprintf(stderr, "Can not malloc(%d LINES*%d CHARS)!\n", MAXLINES2, MAXCHARS); 					return 5; 				} 				buf = buf2; 				MAXLINES = MAXLINES2; 			} 		} 	} 	fclose(fl);     //使用快速排序 	if (ignore_case) qsort(buf, n, MAXCHARS, icompare); 	else qsort(buf, n, MAXCHARS, compare); 	hh = 0;     //读取PROG中内容 	while (1) { 		if (fgets(ln, MAXCHARS, fp) == NULL) break;// 		hh++; 		L = strlen(ln) - 1; 		if ('\n' != ln[L]) {////有限行最后一个字符是'\n',超长行忽略后面内容 			fprintf(stderr, "%s Line %d too long(>%d),spilth ignored.\n", PROG, hh, MAXCHARS); 			while (1) { 				c = fgetc(fp); 				if ('\n' == c || EOF == c) break;//读到'\n'或文件结束符，即表明读完 			} 		} 		while (1) {//去掉行尾的'\n'和空格 			if ('\n' == ln[L] || ' ' == ln[L]) { 				ln[L] = 0; 				L--; 				if (L<0) break;// 			} 			else break;// 		} 		if (L >= 0) { 			if (ignore_case) { 				if (NULL == bsearch(ln, buf, n, MAXCHARS, icompare)) printf("%s\n", ln); 			} 			else { 				if (NULL == bsearch(ln, buf, n, MAXCHARS, compare)) printf("%s\n", ln); 			} 		} 	} 	fclose(fp); 	free(buf); 	return 0; }

user: user-3
content: if (MAXLINES2 == 1280000) MAXLINES2 = 2500000; 这一句有特别的含义吗？

user: user-4
content: 内存中比较啦～

user: user-5
content: #include <stdio.h> #include <stdlib.h>  int file_size(FILE* fp) {     long mark = ftell(fp);     int size;     fseek(fp , 0 , SEEK_END);     size = ftell(fp);     fseek(fp , mark , SEEK_SET);     return size; } int main() {     FILE* f1 , *f2;     FILE* logs;     logs = fopen("log.txt" , "a+");     f1 = fopen("f1.txt" , "r");     f2 = fopen("f2.txt" , "r");      if(!logs)     {         fprintf(stderr , "can`t open log.txt\n");         exit(EXIT_FAILURE);     }     if(!f1)         {             fprintf(logs , "can`t open f1.txt\n");             exit(EXIT_FAILURE);         }     if(!f1)     {         fprintf(logs , "can`t open f2.txt");         exit(EXIT_FAILURE);     }      int a , b;     long n = 0;     size_t s1 , s2;     s1 = file_size(f1);     s2 = file_size(f2);     //比较文件的size     if(s1 != s2)         fprintf(logs,"file size not equel.\n");       //将文件的内容读到内存进行比较     while((a = fgetc(f1)) != EOF && (b = fgetc(f2)) != EOF)     {         n++;         if(a == b)             continue;         else         fprintf(logs, "%ldth byte not equel\n",n);     }       fclose(f1);     fclose(f2);     fclose(logs);     return 0; }

user: user-5
content: 赵老师总是舍不得多敲几个空格和回车，代码挤在一起，看起来很累，坏眼睛啊

user: user-1
content: 凡是不超过200行的代码都不能称之为复杂的代码。

user: user-0
content: 老师，请问您有更简单些的代码示例吗？我只学半年C语言，这种代码有些高深啊TT，做完实验我们就学JAVA了。。。

user: user-1
content: 仅供参考：//输出PROG中有但LIST中没有的文本行，即集合PROG-LIST #include <stdio.h> #include <string.h> #include <stdlib.h> #include <search.h> #define MAXCHARS 512 int MAXLINES=10000,MAXLINES2; char *buf,*buf2; char PROG[256]="PROG";//程序Program需要的文件列表 char LIST[256]="LIST";//dir /b /s生成的实际文件列表List FILE *fp,*fl; int i,c,n,L,hh; int ignore_case=0; char ln[MAXCHARS]; int icompare(const void *arg1,const void *arg2) {    return stricmp((char *)arg1,(char *)arg2); } int compare(const void *arg1,const void *arg2) {    return strcmp((char *)arg1,(char *)arg2); } int main(int argc,char **argv) {     if (argc>1) strcpy(PROG,argv[1]);//命令行参数1覆盖PROG     if (argc>2) strcpy(LIST,argv[2]);//命令行参数2覆盖LIST     if (argc>3) ignore_case=1;//若存在命令行参数3，忽略大小写     if ((fl=fopen(LIST,"rt"))==NULL) {         fprintf(stderr,"Can not open %s\n",LIST);         fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);         return 1;     }     if ((fp=fopen(PROG,"rt"))==NULL) {         fclose(fl);         fprintf(stderr,"Can not open %s\n",PROG);         fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);         return 2;     }     buf=(char *)malloc(MAXLINES*MAXCHARS);     if (NULL==buf) {         fclose(fl);         fclose(fp);         fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);         return 4;     }     n=0;     hh=0;     i=0;     while (1) {         if (fgets(ln,MAXCHARS,fl)==NULL) break;//         hh++;         L=strlen(ln)-1;         if ('\n'!=ln[L]) {//超长行忽略后面内容             fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",LIST,hh,MAXCHARS);             while (1) {                 c=fgetc(fl);                 if ('\n'==c || EOF==c) break;//             }         }         while (1) {//去掉行尾的'\n'和空格             if ('\n'==ln[L] || ' '==ln[L]) {                 ln[L]=0;                 L--;                 if (L<0) break;//             } else break;//         }         if (L>=0) {             strcpy(buf+i,ln);i+=MAXCHARS;             n++;             if (n>=MAXLINES) {                 MAXLINES2=MAXLINES*2;                 if (MAXLINES2==1280000) MAXLINES2=2500000;                 buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);                 if (NULL==buf2) {                     free(buf);                     fclose(fl);                     fclose(fp);                     fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);                     return 5;                 }                 buf=buf2;                 MAXLINES=MAXLINES2;             }         }     }     fclose(fl);     if (ignore_case) qsort(buf,n,MAXCHARS,icompare);     else qsort(buf,n,MAXCHARS,compare);     hh=0;     while (1) {         if (fgets(ln,MAXCHARS,fp)==NULL) break;//         hh++;         L=strlen(ln)-1;         if ('\n'!=ln[L]) {//超长行忽略后面内容             fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",PROG,hh,MAXCHARS);             while (1) {                 c=fgetc(fp);                 if ('\n'==c || EOF==c) break;//             }         }         while (1) {//去掉行尾的'\n'和空格             if ('\n'==ln[L] || ' '==ln[L]) {                 ln[L]=0;                 L--;                 if (L<0) break;//             } else break;//         }         if (L>=0) {             if (ignore_case) {                 if (NULL==bsearch(ln,buf,n,MAXCHARS,icompare)) printf("%s\n",ln);             } else {                 if (NULL==bsearch(ln,buf,n,MAXCHARS,compare)) printf("%s\n",ln);             }         }     }     fclose(fp);     free(buf);     return 0; }