Compare to files,printing the first line where they differ
2012-07-09 16:49
169 查看
#include<stdio.h> # define SIZE 100 int compareTxt(FILE *fp1,FILE *fp2); void show_file(char *name); int main() { FILE *fp1 = fopen("txt1.txt","r"); FILE *fp2 = fopen("txt2.txt","r"); /*place the pointer to the beginning of the file,which point to the file*/ rewind(fp1); rewind(fp2); //show_file("txt2.txt"); compareTxt(fp1,fp2); return 0; } int compareTxt(FILE *fp1,FILE *fp2) { char c1,c2; char *line = new char[SIZE]; while(!feof(fp1) && !feof(fp2)) { if((c1=fgetc(fp1)) != (c2 = fgetc(fp2))) { fgets(line,SIZE,fp1); printf("File 1:%c%s\n",c1,line); fgets(line,SIZE,fp2); printf("File 2:%c%s\n",c2,line); break; } } if(c1 == EOF && c2 == EOF) { printf("Two files are the same."); fclose(fp1); fclose(fp2); return 1; } else if(c1 != EOF && c2 == EOF) { fgets(line,SIZE,fp1); fclose(fp1); fclose(fp2); printf("%s \n",line); return -1; } else { fgets(line,SIZE,fp2); fclose(fp1); fclose(fp2); printf("%s \n",line); return -1; } } void show_file(char *name) { FILE *fp = fopen(name,"r"); char c; while((c = fgetc(fp)) != EOF) { printf("%c",c); } fclose(fp); }
相关文章推荐
- File Comparer - To compare two files and check whether they have the same content
- [每日练习] write a function to find the key in a m*n matrix, where each line and column is incremental
- Export Xellerate configuration objects to XML files from the command-line
- Using the command line to manage files on HDFS--转载
- How to download files from the Linux command line
- Go to the first line OR the last line of the file
- How to extract msu/msp/msi/exe files from the command line
- How to easily open files and URLs from the command line
- MySQL server version for the right syntax to use near 'info where info.stu_id = 1' at line 1
- Select the JavaScript graphing libraries you would like to compare
- sudo apt-get update E: Some index files failed to download. They have been ignored, or old ones use
- Unable to find the ncurses libraries or the required header files解决
- 虚拟机安装麒麟3.2时报unkown filesystem,you need to load the linux kernel first
- error: Your local changes to the following files would be overwritten by merge:
- Importance of Crusher to the Stone Production Line
- 解决“Some index files failed to download, they have been ignored, or old ones used instead.”
- 断句:Store all parameters but the first passed to this function as an array
- How to Change the First Accounting Open Period
- Lab01 Editing text files on the command line
- Cloud 9 IDE 出现“Can\'t render headers after they are sent to the client”错误的临时解决方案