本文介绍了如何简单比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要从两个文件中读取每个字符:PISMENA.TXT和PISMENA2.txt,然后比较它们(一个来自PISMENA,一个来自PISMENA2)。但我在这一部分有一个问题: I need to read every character from a two files: PISMENA.TXT and PISMENA2.txt and then compare them (one from the PISMENA and one from PISMENA2). But I have a problem in this part:while(((fscanf(fr_pismena,"%c",&character_pismena))!=EOF)&&((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF)){ printf("\nZ prveho suboru:%c" "\nZ druheho suboru:%c",character_pismena, character_pismena2); if(character_pismena!=character_pismena2) count_of_differences++; else ; } 因为当PISMENA.TXT有一个(例如)13个字符而PISMENA2.TXT有14个字符的时候程序结束后是第13个字符。 如果两个文件的字符数相同,那么一切都还可以...... 对不起我的英文,我希望你理解...如果您需要完整的代码,我可以发布它。 谢谢because when the PISMENA.TXT has got a (for example) a 13 characters and PISMENA2.TXT has got a 14 characters then end of the program is after 13th character. When both files has got same number of characters then everything is ok...Sorry for my english, I hope, you understand... If you need a full code, I can post it. Thanks推荐答案 // ... (your code at the moment)// When you are here you will have at most ONE file which is not EOF,// so only ONE of the loops will be executedwhile ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){ count_of_differences++;}while ((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){ count_of_differences++;} #include <stdio.h>#include <conio.h>#include <time.h>void delay(unsigned int mseconds){ clock_t goal = mseconds + clock(); while (goal > clock());}int main(){ FILE *fr_pismena,*fr_pismena2; char character_pismena; char character_pismena2; int count_of_differences=0; openfile_1: if((fr_pismena=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA.TXT","r")) == NULL){ printf("\nERROR: Program can't open the file 'PISMENA.TXT' for read. " "Press 'Enter' to try again or 'q' to close the program."); repair_open_1: switch(getchar()){ case 'q':return 0;break; case '\012':goto openfile_1; default :printf("Incorrect character, try again..");goto repair_open_1; } } else printf("\nThe file PISMENA.TXT is successfully opened.\n\n"); openfile_2: if((fr_pismena2=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA2.TXT","r")) == NULL){ printf("\nERROR: Program can't open the file 'PISMENA2.TXT' for read. " "Press 'Enter' to try again or 'q' to close the program."); repair_open_2: switch(getch()){ case 'q':return 0;break; case '\012':goto openfile_2; default :printf("Incorrect character, try again..");goto repair_open_2; } } else printf("\nThe PISMENA2.TXT is successfully opened.\n\n"); while(1){ if(fscanf(fr_pismena,"%c",&character_pismena)!=EOF){ if(fscanf(fr_pismena2,"%c",&character_pismena2)!=EOF){ printf("\nZ prveho suboru:%c" "\nZ druheho suboru:%c",character_pismena, character_pismena2); if(character_pismena!=character_pismena2) count_of_differences++; else ; } else{ ungetc(character_pismena,fr_pismena); break; } } else{ break; } } while((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){ count_of_differences++; } while ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){ count_of_differences++; } if(count_of_differences==0) printf("\n\nSoubory jsou zhodne\n"); else printf("\n\nSoubory se lisi v %d znacich\n",count_of_differences); fclose(fr_pismena); fclose(fr_pismena2); getch(); return 0;}</time.h></conio.h></stdio.h> 这篇关于如何简单比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 15:47