c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
2014-04-06 13:00
405 查看
Write a program to compare two files, printing the first line where they differ.Here's Rick's solution:
/****************************************************** KnR 7-6 -------- Write a program to compare two files and print the first line where they differ. Author: Rick Dearman email: rick@ricken.demon.co.uk Note: This program prints ALL the lines that are different using the <> indicators used by the unix diff command. However this program will not cope with something as simple as a line being removed. In reality the program would be more useful if it searched forward for matching lines. This would be a better indicator of the simple removal of some lines. This has lead me to track down a version of the "diff" command available on GNU/Linux systems. for more information go to the web site at: www.gnu.org ******************************************************/
#include <stdio.h> #include <string.h> #define MAXLINE 1000 void diff_line( char *lineone, char *linetwo, int linenumber ) { if(strcmp (lineone, linetwo) < 0 || strcmp (lineone, linetwo) > 0) printf( "%d<%s\n%d>%s\n", linenumber, lineone, linenumber, linetwo); } int main(int argc, char *argv[] ) { FILE *fp1, *fp2; char fp1_line[MAXLINE], fp2_line[MAXLINE]; int i; if ( argc != 3 ) { printf("differ fileone filetwo\n"); exit(0); } fp1 = fopen( argv[1], "r" ); if ( ! fp1 ) { printf("Error opening file %s\n", argv[1]); } fp2 = fopen( argv[2], "r" ); if ( ! fp2 ) { printf("Error opening file %s\n", argv[2]); } i = 0; while ( (fgets(fp1_line, MAXLINE, fp1) != NULL) && (fgets(fp2_line, MAXLINE, fp2) != NULL)) { diff_line( fp1_line, fp2_line, i ); i++; } return 0; }and here's "flippant squirrel"'s solution:
/* Exercise 7-6 - write a program to compare two files, printing the first line * where they differ * * Note : I amended this a bit...if a file is shorter than the other, but is identical * up to that point, the program prints out "EOF" as the string that's not equal. * */
#include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFF_SIZE 1000 /* uses fgets, removes the '\n' at the end of the string if it exists */ char *safegets(char *buffer, int length, FILE *file) { char *ptr; int len; if (buffer != NULL) { ptr = fgets(buffer, length, file); if (ptr != NULL) { len = strlen(buffer); if (len > 0) { if (buffer[len - 1] == '\n') { buffer[len - 1] = '\0'; } } } return ptr; } return NULL; } int main(int argc, char *argv[]) { FILE *leftFile, *rightFile; char buff1[BUFF_SIZE], buff2[BUFF_SIZE]; char *ptr1, *ptr2; unsigned long lineNum = 0; if (argc < 3) { fprintf(stderr, "Usage : 7_6 <path to file> <path to file>\n"); return 0; } if (!(leftFile = fopen(argv[1], "r"))) { fprintf(stderr, "Couldn't open %s for reading\n", argv[1]); return 0; } if (!(rightFile = fopen(argv[2], "r"))) { fprintf(stderr, "Couldn't open %s for reading\n", argv[2]); fclose(leftFile); /* RJH 10 Jul 2000 */ return 0; } /* read through each file, line by line */ ptr1 = safegets(buff1, BUFF_SIZE, leftFile); ptr2 = safegets(buff2, BUFF_SIZE, rightFile); ++lineNum; /* stop when either we've exhausted either file's data */ while (ptr1 != NULL && ptr2 != NULL) { /* compare the two lines */ if (strcmp(buff1, buff2) != 0) { printf("Difference:\n"); printf("%lu\t\"%s\" != \"%s\"\n", lineNum, buff1, buff2); goto CleanUp; } ptr1 = safegets(buff1, BUFF_SIZE, leftFile); ptr2 = safegets(buff2, BUFF_SIZE, rightFile); ++lineNum; } /* * if one of the files ended prematurely, it definitely * isn't equivalent to the other */ if (ptr1 != NULL && ptr2 == NULL) { printf("Difference:\n"); printf("%lu\t\"%s\" != \"EOF\"\n", lineNum, buff1); } else if (ptr1 == NULL && ptr2 != NULL) { printf("Difference:\n"); printf("%lu\t\"EOF\" != \"%s\"\n", lineNum, buff2); } else { printf("No differences\n"); } CleanUp: fclose(leftFile); fclose(rightFile); return EXIT_SUCCESS; }
相关文章推荐
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- python比较用户输入的两个文件,如果不同,显示所有不同的行号
- 用python比较两个文件中内容的不同之处, 并输出行号和内容.
- 汇编语言:输入两个数,不大于30,有提示,比较两个数字,如果相等,计算和,如果不相等,计算平均值,并且都要以2进制的格式输出,按照升序输出,计算两个数字各占和的百分比,并且按照“%”的格式输出。
- 文件的输入输出(举例,文本文件复制) C 语言
- 比较用户输入的两个文件,显示出所有不同处的行号与第一个不同字符的位置
- 用cmd的FC命令 对比两个文件夹内容不同并将文件名输出到文件中
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- C语言程序设计习题 1-9 编写一个将输入复制到输出的程序,并将其中连续多个空格用一个空格代替
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- 使用shell 统计文本行数,输出两个文件不同或相同的行
- c语言 输入两个浮点数,输出它们中的大数
- 习题 6.14 编一个程序,将两个字符串s1和s2比较,若s1>s2,输出一个正数;若s1=s2, 输出0;若s1<s2,输出一个负数。不要用strcmp函数。两个字符串用gets函数输入。
- 文件输入/输出函数对比
- C语言输入十个数,任意相邻的两个数不同,输出所有的递增,递减序列
- 给定两个输入文本,找出它们共有的最长字符串
- 习题 9.3 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录。
- C语言习题5.22--输出已交换后的两个值
- 2、编写程序,从键盘上输入两个电阻的值,求它们并联和串连的电阻值,输出结果保留两位小数。
- 问题:求两个字符串的最长公共子串。 要求:输入两个字符串,输出它们的最长公共子串,包括长度。