我正在阅读本书,并且遇到了一些例子,我不知道如何从第1章开始测试。他们有你阅读行并寻找不同的角色,但我不知道如何测试我所做的C代码。由K&R编写的C语言编程语言示例CH1
例如:
/* 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;
}
我不知道如何创建具有不同的线路长度,以测试该上的文件。做一些研究,我看到后有人试图这样说:
[[email protected] kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-17.c
[[email protected] 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
[[email protected] kr2]$
但我不知道他是如何对其进行管理。任何帮助将不胜感激。
如果您有配套光盘,您可以从该章节的子目录中的磁盘上的文件中剪切/粘贴数据,然后通过io重定向将其发送到您的程序'bash $ progname
WhozCraig