我被K&R的1.5.2问题困扰了.我google了一段时间后发现我必须在输入字符后提供EOF输入.
long nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
Run Code Online (Sandbox Code Playgroud)
我尝试将commnad-D和control-D作为EOF输入,但没有任何效果.任何想法如何为mac osx提供EOF?
为什么Ctrl+Z在下面的小程序中没有触发循环完成?
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
{
//nothing
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入:test^ZEnter,它不会跳出循环。
我发现了相关问题(此处和此处),但没有一个问题可以解释 Windows 下的 C(而不是 C++)。
注意:我在 Windows 8.1 上使用 Visual Studio 2015 PRE
我的CLI程序在windows上编译并运行良好.在linux上编译很好,但在运行时会导致分段错误.
我转向stackoverflow寻求帮助,发现了一些类似于我要问的建议valgrind的问题,我刚刚安装了它(哇!).
所以我通过valgrind运行我的程序,得到了令人沮丧的大量输出,但我将从第一条错误消息开始:
==11951== Command: ./vt
==11951==
Loading...
Load default database? (y/n)y
Opened input file vtdb.~sv, reading contents...
==11951== Invalid write of size 1
==11951== at 0x400FA9: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== Address 0x53b05bb is 0 bytes after a block of size 11 alloc'd
==11951== at 0x4C28FAC: malloc (vg_replace_malloc.c:236)
==11951== by 0x400EAC: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951==
...finished. …Run Code Online (Sandbox Code Playgroud) int x,y,m;
for(;;){
m=scanf("%d %d",&x,&y);
if (m!=2 || m==EOF){
break;
}
else{
printf("/%d/%d/\n",x,y);
}
}
if (feof ( stdin )){
printf("End of input\n");
}else if(m!=2){
printf("There was an error\n");
}
Run Code Online (Sandbox Code Playgroud)
在linux下ctrl + D表示输入结束,而对于windows ctrl + z应该可以做到这一点,但它不起作用.有任何想法吗?
int main()
{
int c;
c = getchar();
while (c!=EOF){
putchar(c);
c=getchar();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当我输入值-1为什么循环没有被终止.
EOF的值= -1我从这段代码得到的,
main()
{
printf("EOF is %d\n",EOF);
}
Run Code Online (Sandbox Code Playgroud)
当我使用Ctrl+ 时代码被终止D,有没有其他方法可以在不使用Ctrl+的情况下终止相同的代码D.
在以下代码中比较的值是多少?
while ((c = getchar()) != EOF)
if ( c == '\n')
Run Code Online (Sandbox Code Playgroud)
我知道这'\n'是一个常量变量,因为单引号.我知道它代表ASCII表上字符的数值,对吗?这相当于110.但是那是什么
((c = getchar()) != EOF) 返回?
谢谢