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. Where does Windows store MSI files for uninstallation?

    Original link: Where does Windows store MSI files for uninstallation? Following content are only use ...

  2. 第8条:覆盖equals时遵守通用约定

    如果不需要覆盖equals方法,那么就无需担心覆盖equals方法导致的错误. 什么时候不需要覆盖equals方法? 1.类的每个实例本质上是唯一的. 例如对于Thread,Object提供的equa ...

  3. 顺序表 C++模板实现

    #include <iostream> using namespace std; template <typename T> class list{ private: int ...

  4. 掌握 ActionResult

    我在上一篇博客不要停留在表面,MVC 3 我们要深入一些 说明了我们的掌握程度还是不够,还需要我们继续努力.但是有园友质疑说他们认为我说的只是书院派,并不实用,这令作为程序员的我很是生气.好吧,那咱们 ...

  5. win7环境下配置Java环境

    ==下载Java SE Development Kit 8u45== http://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...

  6. android:Faild to install,你的主机中的软件终止了一个连接错误解决

    当在用真机调试android程序时出现Faild to install,你的主机中的软件终止了一个连接错误时可以这样解决: 在手机开启usb调试和安装未知来源软件的情况下: 1:先查进入任务管理器查看 ...

  7. C# zip/unzip with DotNet framework 4.5

    add reference System.IO.Compression.FileSystem public class ZipHelper { public static string UnZip(s ...

  8. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  9. Get your Windows product key from a script

    The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...

  10. python 操作sqlite数据库

    '''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...