fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错为非0 对流stream相关的文件定位,随后的读写操作将从新位置开始. 对于二进制文件,此位置被定位在由origin开始的offset个字符处.origin的值可能为SEEK_SET(文件开始处).SEEK_CUR(当前位置)或SEEK_END(文件结束处). 对于文本流,offset心须为0,或者是由函数f…
Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text. /* This is the first program exercise where the spec isn't entirely * clear. The spec says…
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…
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the storage initialized to zero. Write calloc , by calling malloc or by modifying it. /* Exercise 8.6. The standard library function calloc(n, size) retur…
Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time. #include <stdio.h> #define MAX_LINE 1024 void discardnewline(char s[]) { int i; ; s[i] != '\0'; i++) { if(s[i] ==…
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines. 其实做这道题目有两种思路: 1.后向模式:利用getline()先将输入流中,每一行完全接收,然后从接收的line字符串中末尾,往前扫,直到发现第一个非空格和制表符字符: 2.前向模式:每接收一个字符,都要进行输出.判断. /* K&R2 1-18 p31: Writ…
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. 统计输入中单词的长度,并且绘制相应的直方图.水平的直方图比较容易绘制,垂直的直方图较困难一些. /* This program was the…
How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any? 你会如何测试前面的字符统计程序呢?什么样的测试输入,最能揭示你程序中的bug呢? It sounds like they are really trying to get the programmers to learn how to do a unit test. 这听起…
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 编写这样一个程序,实现将输入流复制到输出流,但是要将输入流中多个空格过滤成一个空格. 1.旗帜变量方法 #include <stdio.h> int main(void) { int c; int inspace; //这里用了旗帜变量来过滤多余空格 inspace = ;…
[序] 上节我们实现了数据结构中最简单的Vector,那么来到第三章,我们需要实现一个Set set的特点是 内部有序且有唯一元素值:同时各种操作的期望操作时间复杂度在O(n·logn): 那么标准的C++ STL(Standard Template Library)  容器内部使用的是什么呢? STL使用的是红黑树或者hash Tree ,由于笔者现在的水平和精力,没时间搞这个啦,于是我就 挑了一个稍微熟悉一点的数据结构:AVL 树: github:https://github.com/Kim…