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) < || strcmp (lineone, linetwo) > )
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 != )
{
printf("differ fileone filetwo\n");
exit();
} fp1 = fopen( argv[], "r" );
if ( ! fp1 )
{
printf("Error opening file %s\n", argv[]);
} fp2 = fopen( argv[], "r" );
if ( ! fp2 )
{
printf("Error opening file %s\n", argv[]);
}
i = ;
while ( (fgets(fp1_line, MAXLINE, fp1) != NULL)
&& (fgets(fp2_line, MAXLINE, fp2) != NULL))
{
diff_line( fp1_line, fp2_line, i );
i++;
} return ;
}

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 > )
{
if (buffer[len - ] == '\n')
{
buffer[len - ] = '\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 = ; if (argc < )
{
fprintf(stderr, "Usage : 7_6 <path to file> <path to file>\n");
return ;
} if (!(leftFile = fopen(argv[], "r")))
{
fprintf(stderr, "Couldn't open %s for reading\n", argv[]);
return ;
} if (!(rightFile = fopen(argv[], "r")))
{
fprintf(stderr, "Couldn't open %s for reading\n", argv[]);
fclose(leftFile); /* RJH 10 Jul 2000 */
return ;
} /* 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) != )
{
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程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号的更多相关文章

  1. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  2. C语言风格字符串的概念、定义、输入字符串、输出字符串

    字符串: C语言中最有用.最重要的数据类型之一. 字符串:是以\0字符结尾的char类型数组.所以可以把数组和指针知识应用于字符串. 如何在程序定义字符串: 1.字符串字面量 用双引号括起来的内容称为 ...

  3. Python笔记_第一篇_面向过程_第一部分_0.开场白

    *什么是Python? Python是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido(吉多) van Rossum于1989年发明,第一个公开版本发行于1991年.在国外应用非常的广泛,国 ...

  4. C程序设计语言(第二版)习题:第一章

    第一章虽然感觉不像是个习题.但是我还是认真去做,去想,仅此而已! 练习 1-1 Run the "hello, world" program on your system. Exp ...

  5. 《C程序设计语言》读书笔记----习题1-21

    题目就不写了,大概意思就是:尽量用制表符'\t'替换掉字符串中的空格. 同学们需要注意的是,打印一个制表符'\t',其所占长度不是固定的. 这里要理解“制表符”和“制表符终止位”.“制表符”的作用是使 ...

  6. 清风注解-Swift程序设计语言:Point11~15

    目录索引 清风注解-Swift程序设计语言 Point 11. 数值型字面量 代码事例: let decimalInteger = // 十进制的17 let binaryInteger = 0b10 ...

  7. Notes 20180506 : Java程序设计语言概述

    2.Java程序设计语言概述 如果对于开发语言的排行榜有所关注的话,那么会发现很长一段时间以来Java都是位居榜首的高级开发语言,作为一个Java开发者,为此感到骄傲的同时也深感忧虑,骄傲的是自己接触 ...

  8. C程序设计语言笔记-第一章

     The C Programming language notes 一 基础变量类型.运算符和判断循环         char                 字符型  character      ...

  9. 人工智能技术导论——逻辑程序设计语言PROLOG

    最近在复习人工智能导论,里面介绍了一种逻辑关系语言PROLOG,但这本书里面用到的编译器是Turbo PROLOG,这个编译器早就被淘汰了,我后来找的了它的升级版Visual PROLOG,但一些语法 ...

随机推荐

  1. 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)

    问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...

  2. centos7下编译qt的mysql驱动

    在编译mysql驱动之前,首先要安装mysql,可以使用yum安装,这里将不再介绍. 在将qt和mysql都安装好之后,首先找到mysql的头文件以及他的共享库,我的mysql是使用yum安装的,头文 ...

  3. Beyond Compare 使用介绍

    Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...

  4. 一个页面,多个flash(刚学jq插件)

    只贴js那部分哦 调用 // flash轮播图 var sumF=$('.btnTabs span').length/4; //有四个flash var flashT01=new flash($('. ...

  5. C#多线程(一)

    一.定义与理解 1.定义 线程是操作系统分配CPU时间片的基本单位,每个运行的引用程序为一个进程,这个进程可以包含一个或多个线程. 线程是进程中的执行流程,每个线程可以得到一小段程序的执行时间,在单核 ...

  6. struct--file_operations

    struct--file_operations-----------------------------------------    struct file_operations是一个字符设备把驱动 ...

  7. Render Texture的使用(截取rendertexture的一帧到Texture2D)

    游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用unity搭建一个照 ...

  8. MJRefreshFooterView

    实例化header和footer _header = [MJRefreshHeaderView header]; _header.scrollView = _tableView; 设置header和f ...

  9. RPM是RedHat Package Manager(RedHat软件包管理工具)

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  10. 异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null

    nutch 运行时异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null 参考 ...