K&R,找到最长的线路,相同的代码但不起作用?
K&R, find longest line, same code but not working?
我刚开始阅读 K&R,在第 32-33 页,有一段代码
finds the longest line among the inputs.
我几乎完全复制粘贴了书中给出的代码,只是添加了一些注释行以使代码对我来说更容易理解。但它不起作用。
编辑:不好意思提问。当我按下 Ctrl + Z 以终止程序时,程序似乎无法正常运行。无论我输入多少行,按多少次 Ctrl + Z,它都没有任何反应。
以下是我的代码版本:
/* Find the longest line among the giving inputs and print it */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getLine(char line[], int maxLine);
void copy(char to[], char from[]);
int main(void) {
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here*/
max = 0;
/* getLine function takes all the input from user, returns it's size and equates it to the variable len
* Then, len is compared whether it's greater than zero because if there's no input, no need to do any calculation
* EDGE CASE
*/
while ((len = getLine(line, MAXLINE)) > 0)
/* If the length of input is larger than the previous max length, set max as the new length value and copy that input */
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line, EDGE CASE */
printf("%s", longest);
return 0;
}
/* Read a line into s, return length.
* Since the input length is unknown, there should be a limit */
int getLine(char s[], int lim) {
int c, i;
/* The loop's first condition is whether the input length is below the limit. EDGE CASE
* If it's not, omit the rest because it would cause a BUFFER OVERFLOW. Next, take the input as long as it's not an EOF command.
* Finally, if the input is end of line, finish the loop, don' take it.
*/
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n')
s[i++] = c;
s[i++] = '[=10=]'; // always put a '[=10=]' character to a string array ending, so that the compiler knows it's a string.
return i;
}
void copy(char to[], char from[]) {
int i = 0;
// This loop is readily assigns all chars from the source array to the target array until it reaches the ending char.
while ((to[i] = from[i]) != '[=10=]')
++i;
}
提前致谢!
好的,这是错误:
s[i++] = '[=10=]'; // always put a '[=10=]' character to a string array ending, so that the compiler knows it's a string.
即使没有输入(当它直接得到 EOF
时),这也会导致它终止字符串,并且由于它在 return 之前递增 i
,getLine()
永远不会 return 0 因此 main()
永远不会停止。删除 ++
修复它,用于我的简单测试。
此外,评论具有误导性,编译器什么都不知道。当代码运行s时,编译器不再存在;字符串的内存格式是让 运行 时代的图书馆满意所需要的,因为这是他们所期望的。
我刚开始阅读 K&R,在第 32-33 页,有一段代码
finds the longest line among the inputs.
我几乎完全复制粘贴了书中给出的代码,只是添加了一些注释行以使代码对我来说更容易理解。但它不起作用。
编辑:不好意思提问。当我按下 Ctrl + Z 以终止程序时,程序似乎无法正常运行。无论我输入多少行,按多少次 Ctrl + Z,它都没有任何反应。
以下是我的代码版本:
/* Find the longest line among the giving inputs and print it */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getLine(char line[], int maxLine);
void copy(char to[], char from[]);
int main(void) {
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here*/
max = 0;
/* getLine function takes all the input from user, returns it's size and equates it to the variable len
* Then, len is compared whether it's greater than zero because if there's no input, no need to do any calculation
* EDGE CASE
*/
while ((len = getLine(line, MAXLINE)) > 0)
/* If the length of input is larger than the previous max length, set max as the new length value and copy that input */
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line, EDGE CASE */
printf("%s", longest);
return 0;
}
/* Read a line into s, return length.
* Since the input length is unknown, there should be a limit */
int getLine(char s[], int lim) {
int c, i;
/* The loop's first condition is whether the input length is below the limit. EDGE CASE
* If it's not, omit the rest because it would cause a BUFFER OVERFLOW. Next, take the input as long as it's not an EOF command.
* Finally, if the input is end of line, finish the loop, don' take it.
*/
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n')
s[i++] = c;
s[i++] = '[=10=]'; // always put a '[=10=]' character to a string array ending, so that the compiler knows it's a string.
return i;
}
void copy(char to[], char from[]) {
int i = 0;
// This loop is readily assigns all chars from the source array to the target array until it reaches the ending char.
while ((to[i] = from[i]) != '[=10=]')
++i;
}
提前致谢!
好的,这是错误:
s[i++] = '[=10=]'; // always put a '[=10=]' character to a string array ending, so that the compiler knows it's a string.
即使没有输入(当它直接得到 EOF
时),这也会导致它终止字符串,并且由于它在 return 之前递增 i
,getLine()
永远不会 return 0 因此 main()
永远不会停止。删除 ++
修复它,用于我的简单测试。
此外,评论具有误导性,编译器什么都不知道。当代码运行s时,编译器不再存在;字符串的内存格式是让 运行 时代的图书馆满意所需要的,因为这是他们所期望的。