c - getchar 缓冲输入、EOF 和终端驱动程序

标签 c getchar putchar

我正在尝试了解终端驱动程序如何与 getchar 一起工作。以下是我在阅读 KandR 时编写的一些示例代码:

代码 1:

#include <stdio.h>

int main(){

  int c = getchar();
  putchar(c);


  return 0;

}

代码 2:

    #include <stdio.h>

    int main(){

    int c = EOF;

    while((c=getchar()) != EOF){
    printf("%c",c);
    }
    return 0;
}

代码 3: //模拟wc命令功能的准系统程序

    #include <stdio.h>
    #define IN 1
    #define OUT 0

    int main(){
      //nc= number of characters, ns = number of spaces, bl=number of newlines, nw=number of words
      int c = EOF,nc=0,nw=0,ns=0,nl=0, state = OUT;

      while((c=getchar())!=EOF){
        ++nc;
        if(c=='\n'){
          ++nl;
          state = OUT;
        }
        else if(c==' '){
          ++ns;
          state = OUT;
        }

        else{
          if(state == OUT){
          state = IN;
          ++nw;}
        }

      }

      printf("\n%d %d %d %d",nc,nw,ns,nl); 
return 0;
}

我想了解终端驱动程序何时真正将输入字符串交给程序。假设我输入的是字符串“this is a test”,然后按回车键,那么上面提到的代码是这样工作的:

代码 1:输出“t”(程序结束)

代码 2:输出“这是一个测试”,跳转到下一行(因为它也输出我按下的回车)并再次等待输入。

代码 3:对于上面的字符串后跟一个回车,不输出任何内容。我需要按 Ctrl+D 才能显示输出(输出为 15 4 3 1)

1) 为什么在代码 3 的情况下我需要明确地按 Ctrl+D (EOF) 才能将输入发送到我的程序?换句话说,为什么我的输入字符串在我按下回车键后发送到我的程序,如果是代码 1 和代码 2?为什么它不要求 EOF?

2) 此外,在代码 3 的情况下,如果我在输入字符串后不按 enter,我需要按两次 Ctrl+D 才能显示输出。为什么会这样?

编辑:

对于另一个输入说“TESTING^D”,上面的代码是这样工作的:

1) 输出“T”并结束

2) 输出“TESTING”并等待更多输入

3) 在按下另一个 Ctrl+D 之前不输出任何内容。然后它输出 7 1 0 0。

对于此输入,在代码 1 和代码 2 的情况下,当收到 Ctrl+D 时,终端驱动程序将输入字符串发送到程序。这是否意味着/n 和 Ctrl+D 的处理方式相同,即它们两者都作为终端驱动程序将输入发送到程序的标记?那为什么我需要在第二种情况下按两次 Ctrl+D 呢?

http://en.wikipedia.org/wiki/End-of-file表示驱动程序在换行时将 Ctrl+D 转换为 EOF。但是在我的“TESTING^D”输入的情况下,即使 ^D 与输入的其余部分位于同一行,它也能正常工作。对此可能的解释是什么?

最佳答案

一般信息:

案例代码 2:您还需要执行 ctrl+D 才能退出。

实际上 EOF 是通过按 ctrl+D 实现的,所以你的 while 循环条件是这样说的:

  1. 从键盘获取输入
  2. 存储在c中
  3. 如果输入不等于 EOF 则执行 while 循环体

EOF 只是整数 -1,这可以在终端中通过按 ctrl+D 来实现。以这个例子为例:

while((c=getchar()) != EOF){
   // execute code
}

printf("loop has exited because you press ctrl+D");

条件继续接收输入,但在您按下 ctrl+D 时停止,然后继续执行其余代码。

回答您的问题:

1) Why in case of code 3 do i need to press Ctrl+D (EOF) explicitly for the input to be sent to my program? To put this in other words, why was my input string sent to my program in case of code 1 and code 2 after i pressed enter? Why didn't it also ask for EOF?

在代码 2 和 3(不仅是 3)中,您需要按 Ctrl+D,因为 while 循环仅在读取到 EOF 时才停止从键盘获取输入。在代码 1 中你没有循环,所以当你输入一个或多个字符时,程序将读取输入的字符但只会存储第一个字符,然后它会打印它并终止程序,所以在这种情况下不需要 EOF因为你不是在任何情况下在任何地方要求它。

2) Also, in case of code 3, if i do not press enter after the input string, i need to press Ctrl+D twice for the output to be displayed. Why is this the case?

如果您在程序需要输入时开始键入,那么在键入至少一个字符后按 ctrl+D,这将告诉程序停止接受输入并返回输入的字符。之后,如果之前没有输入任何字符,再按ctrl+D,会返回EOF,不满足while循环的条件,跳转继续执行剩下的代码

关于c - getchar 缓冲输入、EOF 和终端驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226983/

相关文章:

c - 在 C 结构上使用 free

process - 如何向通过 fork 和 execl 创建的子进程发送信号?

无法从 Matlab Coder 创建 .dll

c - 为什么我们使用 scanf() 却需要 getchar()?

c - getchar() != EOF

c - `putchar` 的以下两种用法有什么区别?

c - 将数组传递给函数,file.exe 停止工作

c - STDIN 缓冲区和 getchar() 指针在连续调用期间如何变化?

c - while(scanf) : why does using getchar() keep the input going 输入问题

字符数组读取 3 个输入而不是 c 中的 5 个输入?