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,但一些语法 ...
随机推荐
- vs 2010 Cannot find or open the PDB file
打开VS2010:工具-->选项-->>调试-->符号接下来就是选择Microsoft,然后确认 接着随便编译一个程序,过程会灰常的慢. 看到此目录下符号缓存了吗?C:\Us ...
- C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)
以Fruit和Apple为例进行分析: Fruit和Apple的定义如下: 通过在两种编译环境下的测试(GNU GCC & VS2015),可以发现这两种编译器的对象模型是一样的,如下图所示: ...
- libjingle线程机制
libjingle包装了所有的线程,包括signaling thread,worker thread, 和其它任何线程,用talk_base::Thread来包装.所有的 Thread对象由Threa ...
- N个数随机相加得出固定值的排列组合
static double[] iArr = new double[10] { 1,2,3,4,5,6,7,8,9,10 }; static Stack<double> stack = n ...
- mysql---多表关联
首先要介绍一下集合的概念:集合具有无序性.唯一性. 无序性:指集合内部元素没有相对顺序的概念,对于两个集合而言,只要元素值和元素个数相同则两个集合相等. 唯一性:指集合内部元素不存在值相等的元素. 上 ...
- Web前端新人笔记之jquery入门
本章将为大家介绍以下几点内容: 1.jquery的主要特点: 2.建立jquery的编码环境: 3.简单jquery脚本示例: 4.选择jquery而不是纯javaScript的理由: 5.常用的jq ...
- HttpContext.Current多线程调用
1.在web网站的Global中,进行数据量比较大的初始化工作,而为了使用户在页面上能够及时响应,我们在Global中开启了一个线程执行该函数模块. 不过,在线程中用到HttpContext.Curr ...
- C#实现登录窗口(不用隐藏)
C#登录窗口的实现,特点就是不用隐藏,感兴趣的朋友不要错过 (1).在程序入口处,打开登录窗口 复制代码代码如下: static void Main() { Application.EnableV ...
- Dede 列表页 缩略图 有显示无则不显示
[field:array runphp='yes']@me = (strpos(@me['litpic'],'defaultpic') ? "" : "<div c ...
- 初试mysql存储过程&触发器
玩mysql以来,一直没有试过实现存储过程,因为存储过程的语法看起来有些笨重.所以一直采用手动批量运行查询,而且要手动改日期之类的参数. 今天尝试着学了一会,发现其实是很简单的.语法上确实格式复杂些, ...