我正在尝试理解 K&R 书中的示例 1.9,但我不知道如何发送 EOF。一些消息来源提到了 Ctr+Z,但这只会终止程序。我以某种方式成功地通过 Enter 和 Ctrl+Z 以及 Ctrl+V 的组合发送了 EOF,但我无法重现它。
#include <stdio.h>
#define MAXLINE 1000
main()
{
int len;
int max;
char line[MAXLINE];
char save[MAXLINE];
max = 0;
while((len = getline_my(line, MAXLINE)) > 0)
if(len > max) {
max = len;
copy(line, save);
}
if(max > 0)
printf("%s", save);
}
getline_my(s, lim)
char s[];
int lim;
{
int c, i;
for(i=0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)// As long as the condition is fulfilled
s[i] = c;
if (c == '\n') {
s[i] = c;
i++;
}
s[i] = '\0';
return(i);
}
copy(s1, s2)
char s1[];
char s2[];
{
int i;
i = 0;
while((s2[i] = s1[i]) != '\0')
i++;
}
最佳答案
您可以使用 CTRL+D
(对于 *nix)或 CTRL+Z
模拟 EOF,然后从命令行输入(对于 Windows)。
关于c - 如何通过 Windows 终端发送 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136400/