我正在阅读这本书,并且遇到了一些我不知道如何从第 1 章测试的示例。它们让你逐行阅读并寻找不同的字符,但我不知道如何测试我制作的 C 代码.
例如:
/* K&R2: 1.9, Character Arrays, exercise 1.17
STATEMENT:
write a programme to print all the input lines
longer thans 80 characters.
*/
<pre>
#include<stdio.h>
#define MAXLINE 1000
#define MAXLENGTH 81
int getline(char [], int max);
void copy(char from[], char to[]);
int main()
{
int len = 0; /* current line length */
char line[MAXLINE]; /* current input line */
while((len = getline(line, MAXLINE)) > 0)
{
if(len > MAXLENGTH)
printf("LINE-CONTENTS: %s\n", line);
}
return 0;
}
int getline(char line[], int max)
{
int i = 0;
int c = 0;
for(i = 0; ((c = getchar()) != EOF) && c != '\n' && i < max - 1; ++i)
line[i] = c;
if(c == '\n')
line[i++] = c;
line[i] = '\0';
return i;
}
我不知道如何创建一个具有不同行长的文件来测试它。在做了一些研究之后,我看到有人这样尝试:
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-17.c
[arch@voodo kr2]$ ./a.out
like htis
and
this line has more than 80 characters in it so it will get printed on the terminal right
now without any troubles. you can see for yourself
LINE-CONTENTS: this line has more than 80 characters in it so it will get printed on the
terminal right now without any troubles. you can see for yourself
but this will not get printed
[arch@voodo kr2]$
但我不知道他是如何管理的。任何帮助将不胜感激。