我试图理解K&R书中的例1.9,但我不知道如何发送EOF.一些消息来源提到了Ctr + Z,但这只是终止了程序.我以某种方式设法发送EOF与Enter和Ctrl + Z的组合,也许Ctrl + V,但我无法重现它.
#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++;
}
Run Code Online (Sandbox Code Playgroud)
xag*_*gyg 62
您可以从命令行使用CTRL+D
(for*nix)或CTRL+Z
(对于Windows)模拟EOF .
在寡妇中,当您准备完成输入时,Enter
按键然后按Ctrl+Z
,然后Enter
完成输入.
int main(){
char ch[100];
scanf("%[^EOF]",ch);
printf("\nthe string is:\n%s\n",ch);
fflush(stdin);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37060 次 |
最近记录: |