C程序设计:如何读取来自stdin的终端输入?

时间:2021-08-22 00:10:33

So, I'm trying to write a c program that reads input piped into the program (through stdin), but I also need to be able to read input from the terminal (so I obviously can't read it from stdin). How would I do that? I'm trying to open another file handle to /dev/tty like this:

因此,我正在尝试编写一个c程序,它读取输入管道进入程序(通过stdin),但是我还需要能够从终端读取输入(因此我显然不能从stdin中读取它)。我该怎么做呢?我正在尝试打开另一个文件句柄到/dev/tty,就像这样:

int see_more() {
    char response;
    int rd = open("/dev/tty", O_RDWR);
    FILE* reader = fdopen(rd, "r");
    while ((response = getc(reader)) != EOF) {
        switch (response) {
            case 'q':
                return 0;
            case ' ':
                return 1;
            case '\n':
                return -1;
        }
    }
}

But that results in a segmentation fault.

但这导致了分割错误。

Here's the version that works. Thanks for everyone's help :)

这是有效的版本。谢谢大家的帮助

int see_more() {
    char response;
    while (read(2, &response, 1)) {
        switch (response) {
            case 'q':
                return 0;
            case ' ':
                return 1;
            case '\n':
                return -1;
        }
    }
}

2 个解决方案

#1


3  

The problem is that you're using single quotes instead of double quotes:

问题是你使用的是单引号而不是双引号:

FILE* reader = fdopen(rd, 'r');

should be

应该是

FILE* reader = fdopen(rd, "r");

Here is the prototype of fdopen:

以下是fdopen的原型:

FILE *fdopen(int fildes, const char *mode);

It expects a char*, but you're passing it a char.

它需要一个char*,但您正在传递它一个char。

#2


0  

If you have another input piped in, then that will replace stdin (the terminal) with that input file. If you'd like to get input from the terminal, I'd suggest taking the file in as a parameter rather than a pipe, and then you can use stdin as normal.

如果您有另一个输入管道,那么将用该输入文件替换stdin(终端)。如果您想从终端获得输入,我建议将文件作为参数而不是管道,然后您可以使用stdin作为正常。

Here's an example.

这是一个例子。

Execution:

执行:

./a.out foo.txt

Code:

代码:

int main(int argc, char* argv[])
{
    if (argc >= 2)
    {
        char* filename = argv[1];
    }
    else
    {
        // no filename given
        return 1;
    }

    // open file and read from that instead of using stdin
    // use stdin normally later
}

#1


3  

The problem is that you're using single quotes instead of double quotes:

问题是你使用的是单引号而不是双引号:

FILE* reader = fdopen(rd, 'r');

should be

应该是

FILE* reader = fdopen(rd, "r");

Here is the prototype of fdopen:

以下是fdopen的原型:

FILE *fdopen(int fildes, const char *mode);

It expects a char*, but you're passing it a char.

它需要一个char*,但您正在传递它一个char。

#2


0  

If you have another input piped in, then that will replace stdin (the terminal) with that input file. If you'd like to get input from the terminal, I'd suggest taking the file in as a parameter rather than a pipe, and then you can use stdin as normal.

如果您有另一个输入管道,那么将用该输入文件替换stdin(终端)。如果您想从终端获得输入,我建议将文件作为参数而不是管道,然后您可以使用stdin作为正常。

Here's an example.

这是一个例子。

Execution:

执行:

./a.out foo.txt

Code:

代码:

int main(int argc, char* argv[])
{
    if (argc >= 2)
    {
        char* filename = argv[1];
    }
    else
    {
        // no filename given
        return 1;
    }

    // open file and read from that instead of using stdin
    // use stdin normally later
}