我正在閱讀本書,並且遇到了一些例子,我不知道如何從第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