【问题标题】:C no EOF signalC 无 EOF 信号
【发布时间】:2019-01-28 16:20:22
【问题描述】:

我正在尝试制作一个程序来计算输入的字符数,但 EOF 信号似乎有问题,因为在我按 Enter 后程序没有结束。我不想使用标志来手动终止它。

我尝试过使用 CTRL+Z 和 CTRL+X 、 CTRL+D 或输入 -1,但都没有奏效。

#include <stdio.h>

void main()
{
  double count;
    for(count=0; (getchar())!=EOF; ++count)
    {
          ;
    }
    printf("Char Count%.0f\n",count);

}

我使用的是 Windows 10、Atom 编辑器、gpp-compiler (3.0.7) 包(由 kriscross07 提供)和 minGW 8.2.0。

【问题讨论】:

  • Windows 上的 ctrl-c
  • @ryyker ctrl-c 不发送中断信号?
  • @bruno - 程序完全按照编写的方式在 WINdows 上使用 ctrl-c 中断。
  • @ryyker 所以这不是 OP 想要的
  • 除此之外,为什么要使用 double 而不是 int(或 unsigned int)来计数?

标签: c mingw atom-editor eof


【解决方案1】:

如果您从命令提示符运行程序,它工作得很好 - 但如果您通过 IDE 启动程序,它将无法工作。

int main()
{
    int c;
    while((c = getchar()) != EOF)
    {
        printf("Char %d read\n", c);
    }
    printf("Exiting .... \n");
    return 0;
}

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 2013-07-14
    • 1970-01-01
    • 2019-03-18
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多