简体   繁体   English

为什么 EOF (ctrl+d) 在 function 代码中不执行? 而且只有 ctrl+z

[英]Why doesn't EOF (ctrl+d) execute in function code? And only ctrl+z

I have some code below that works fine but only exits out when I do ctrl+z on Linux but not ctrl+d (EOF)?我下面有一些代码可以正常工作,但只有当我在 Linux 上执行 ctrl+z 而不是 ctrl+d (EOF) 时才会退出? Does it have something to do with ncurses?它与ncurses有关吗? What should I use instead (ERR?) and why?我应该改用什么(ERR?),为什么?


#include <stdio.h>
#include <ncurses.h>            
#define MAXLINE 10

// count number of chars, once it reaches certain amount
int main (void) 
{
    //cbreak();
    // to open curses terminal
    initscr();

    int i, c;

    // first iteration set to 1
    for (i = 0; (c = getch()) != EOF; i++)
    {
         if (i == (MAXLINE-1))
         {
             printf("\r\n");
             i = -1;
         }
    }

    // to close curses terminal
    endwin();
}

Thank you.谢谢你。

When you type Ctrl-z , the program didn't exit, only stays in background.当您键入Ctrl-z时,程序没有退出,只停留在后台。 You can see it with jobs command.您可以使用jobs命令查看它。 You can exit the program with Ctrl-d by this code:您可以通过以下代码使用Ctrl-d退出程序:

#include <stdio.h>
#include <ncurses.h>            
#define MAXLINE 10

// count number of chars, once it reaches certain amount
int main (void) 
{
    //cbreak();
    // to open curses terminal
    initscr();

    int i, c;

    // first iteration set to 1
    for (i = 0; (c = getch()) != 4; i++)
    {
        if  (i == (MAXLINE-1)) {
            printf("\r\n");
            i = -1;
        }
    }

    // to close curses terminal
    endwin();
}

The change is in [(c = getch()) != 4 ].变化在于 [(c = getch()) != 4 ]。 The code for Ctrl-d is 4. Ctrl-d的代码是 4。

"Why doesn't EOF ( CTRL + D ) execute in function code?" “为什么EOF ( CTRL + D ) 不在 function 代码中执行?”

Because you commented out the cbreak() option and with that the terminal is in raw mode.因为您注释掉了cbreak()选项并且终端处于raw模式。

Quotes from the Linux man pages (emphasize mine): Linux 手册页中的引用(强调我的):

The raw and noraw routines place the terminal into or out of raw mode. raw 和 noraw 例程将终端置于或退出原始模式。 Raw mode is similar to cbreak mode, in that characters typed are immediately passed through to the user program.原始模式类似于 cbreak 模式,因为键入的字符会立即传递给用户程序。 The differences are that in raw mode, the interrupt, quit, suspend, and flow control characters are all passed through uninterpreted, instead of generating a signal .不同之处在于,在原始模式下,中断、退出、挂起和流控制字符都是未经解释地传递的,而不是生成信号

Source: https://linux.die.net/man/3/raw来源: https://linux.die.net/man/3/raw


Initially the terminal may or may not be in cbreak mode, as the mode is inherited;最初终端可能处于也可能不处于 cbreak 模式,因为该模式是继承的; therefore, a program should call cbreak or nocbreak explicitly.因此,程序应该显式调用 cbreak 或 nocbreak。 Most interactive programs using curses set the cbreak mode.大多数使用 curses 的交互式程序都设置了 cbreak 模式。 Note that cbreak overrides raw.请注意,cbreak 会覆盖 raw。

Source: https://linux.die.net/man/3/cbreak来源: https://linux.die.net/man/3/cbreak

As the quotes says, In raw mode the interrupt, quit, suspend, and flow control characters are passed through uninterpreted and do not generate the respective signals.正如引号所说,在raw模式下,中断、退出、挂起和流控制字符未经解释地通过,并且不会生成相应的信号。

cbreak() overrides raw mode. cbreak()覆盖raw模式。 Remove the // preceding cbreak() to uncomment cbreak() and it shall work as desired.删除//前面的cbreak()以取消注释cbreak() ,它将按需要工作。

Beside that, you always should call either cbreak() or nocbreak() explicitly because it isn´t determined in what mode the terminal is at program startup.除此之外,您始终应该显式调用cbreak()nocbreak() ,因为在程序启动时无法确定终端处于何种模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM