EOF 是如何在基于 c 的语言中生成和解释的?
How is EOF generated and interpreted in c based languages?
#include <stdio.h>
int main(){
int c, nl=0;
while((c = getchar()) != EOF)
if(c=='\n')
nl++;
printf("%d",nl+1);
return 0;
}
在 Ubuntu 18.04,GCC 7.3.0
当 getchar()
与 EOF
进行比较时,控制台没有输出。它适用于其他角色。使用 CTRL^D
以 'code 0' 退出程序,控制台上没有任何输出。我已经在 bash 中尝试 运行 但仍然没有输出。
输入:
line 1
line 2
line 3
line 4
line 5
预期输出:
5
实际输出:
program exited with code 0
在 *nix 系统上 EOF
由 Ctrl^D
生成,而在 Windows 系统上 EOF
由 Ctrl^Z
生成。我假设您正在使用 Windows 系统。参见 https://en.wikipedia.org/wiki/End-of-file
这些程序用于从文件中读取输入。以此类推 bash 如果你这样做:
./linecount < textfile.txt
会给你输出
5
但我想在编辑器中使用 ctrl^D
到 generate/trigger EOF
字符效果不佳。(至少在我身上不行)。
#include <stdio.h>
int main(){
int c, nl=0;
while((c = getchar()) != EOF)
if(c=='\n')
nl++;
printf("%d",nl+1);
return 0;
}
在 Ubuntu 18.04,GCC 7.3.0
当 getchar()
与 EOF
进行比较时,控制台没有输出。它适用于其他角色。使用 CTRL^D
以 'code 0' 退出程序,控制台上没有任何输出。我已经在 bash 中尝试 运行 但仍然没有输出。
输入:
line 1
line 2
line 3
line 4
line 5
预期输出:
5
实际输出:
program exited with code 0
在 *nix 系统上 EOF
由 Ctrl^D
生成,而在 Windows 系统上 EOF
由 Ctrl^Z
生成。我假设您正在使用 Windows 系统。参见 https://en.wikipedia.org/wiki/End-of-file
这些程序用于从文件中读取输入。以此类推 bash 如果你这样做:
./linecount < textfile.txt
会给你输出
5
但我想在编辑器中使用 ctrl^D
到 generate/trigger EOF
字符效果不佳。(至少在我身上不行)。