相关疑难解决方法(0)

mac osx终端中的信号EOF

我被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?

c kernighan-and-ritchie

36
推荐指数
3
解决办法
3万
查看次数

为什么 Ctrl-Z 不会触发 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

c eof getchar visual-c++

6
推荐指数
2
解决办法
1万
查看次数

尝试使用valgrind的输出调试ac程序分段错误

我的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)

c debugging valgrind segmentation-fault

4
推荐指数
1
解决办法
1872
查看次数

C - Feof(stdin) - 如何表示Windows中的输入结束?

    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应该可以做到这一点,但它不起作用.有任何想法吗?

c windows stdin eof

2
推荐指数
1
解决办法
9726
查看次数

为什么循环没有被终止?

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.

c

1
推荐指数
1
解决办法
100
查看次数

存储在非EOF变量中的值是多少?

在以下代码中比较的值是多少?

while ((c = getchar()) != EOF)
        if ( c == '\n')
Run Code Online (Sandbox Code Playgroud)

我知道这'\n'是一个常量变量,因为单引号.我知道它代表ASCII表上字符的数值,对吗?这相当于110.但是那是什么

((c = getchar()) != EOF) 返回?

谢谢

c

0
推荐指数
1
解决办法
87
查看次数