c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
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_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号的更多相关文章
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- C语言风格字符串的概念、定义、输入字符串、输出字符串
字符串: C语言中最有用.最重要的数据类型之一. 字符串:是以\0字符结尾的char类型数组.所以可以把数组和指针知识应用于字符串. 如何在程序定义字符串: 1.字符串字面量 用双引号括起来的内容称为 ...
- Python笔记_第一篇_面向过程_第一部分_0.开场白
*什么是Python? Python是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido(吉多) van Rossum于1989年发明,第一个公开版本发行于1991年.在国外应用非常的广泛,国 ...
- C程序设计语言(第二版)习题:第一章
第一章虽然感觉不像是个习题.但是我还是认真去做,去想,仅此而已! 练习 1-1 Run the "hello, world" program on your system. Exp ...
- 《C程序设计语言》读书笔记----习题1-21
题目就不写了,大概意思就是:尽量用制表符'\t'替换掉字符串中的空格. 同学们需要注意的是,打印一个制表符'\t',其所占长度不是固定的. 这里要理解“制表符”和“制表符终止位”.“制表符”的作用是使 ...
- 清风注解-Swift程序设计语言:Point11~15
目录索引 清风注解-Swift程序设计语言 Point 11. 数值型字面量 代码事例: let decimalInteger = // 十进制的17 let binaryInteger = 0b10 ...
- Notes 20180506 : Java程序设计语言概述
2.Java程序设计语言概述 如果对于开发语言的排行榜有所关注的话,那么会发现很长一段时间以来Java都是位居榜首的高级开发语言,作为一个Java开发者,为此感到骄傲的同时也深感忧虑,骄傲的是自己接触 ...
- C程序设计语言笔记-第一章
The C Programming language notes 一 基础变量类型.运算符和判断循环 char 字符型 character ...
- 人工智能技术导论——逻辑程序设计语言PROLOG
最近在复习人工智能导论,里面介绍了一种逻辑关系语言PROLOG,但这本书里面用到的编译器是Turbo PROLOG,这个编译器早就被淘汰了,我后来找的了它的升级版Visual PROLOG,但一些语法 ...
随机推荐
- (转)MySQL Workbench的使用教程 (初级入门版)
转自:http://www.cnblogs.com/yqskj/archive/2013/03/01/2938027.html MySQL Workbench 是 MySQL AB 最近释放的可视数据 ...
- OpenJudge 2694 逆波兰表达式
1.链接地址: http://bailian.openjudge.cn/practice/2694/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算 ...
- ubuntu的syslog为空,停止写入解决方法
修改syslog权限: chown syslog:adm syslog
- CentOS下Apache+SVN+LDAP的安装与配置
上班接近4个月了,在公司做配置管理工程师,主要是在Linux下对公司的源代码以及项目发布进行管理.4个月接触了好多新知识,也对各种工具的集成使用搞得云里来雾里去的,所以打算自己搭建一套环境,进行测试. ...
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- easyui 初体验
简介 jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编写复杂的javas ...
- rpm方式安装gcc缺少依赖项的解决方法
使用rpm方式安装gcc时,有时会报缺少依赖项: libmpfr.so.1 is needed by cpp-4.4.4-13.el6.i686 libppl.so.7 is needed by cl ...
- Linux下实现流水灯等功能的LED驱动代码及测试实例
驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...
- SDC(7) -- 关于使能信号的时序放松
先看下图: 假如使能信号的有效时间为时钟周期的2倍,此时需要使用 set_multicycle_path 放松使能信号 sel_xy_nab ,若是每个寄存器使能端都约束一遍,那就太麻烦了: 这时可以 ...
- 破坏之王——ddos攻击与防范 读书笔记
好久没写博客了,最近把绿盟的<破坏之王——ddos攻击与防范>又翻了一下,整理了关于DDOS分类和原理的xmind图~~ 百度云盘:http://pan.baidu.com/s/1i3ms ...