Linux 在 C 中等待用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10575478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for user input in C?
提问by biggles5107
I'm trying to make a simple command that pauses for user input. I think it'll be useful in Bash scripts.
我正在尝试制作一个简单的命令来暂停用户输入。我认为它在 Bash 脚本中会很有用。
Here's my code:
这是我的代码:
#include <stdio.h>
int main() {
char key[1];
puts("Press any key to continue...");
fgets(key,1,stdin);
}
It doesn't even pause for user input.
它甚至不会因为用户输入而暂停。
I tried earlier to use getch() (ncurses). What happened is, the screen went blank and when I pressed a key, it went back to what was originally on the screen, and I saw:
我之前尝试过使用 getch() (ncurses)。发生的事情是,屏幕变黑了,当我按下一个键时,它又回到了屏幕上原来的样子,我看到:
$ ./pause
Press any key to continue...
$
It's somewhat what I wanted. But all I want is the equivalent of the pause
command in DOS/Windows (I use Linux).
这有点是我想要的。但我想要的只是pause
DOS/Windows中的命令(我使用 Linux)。
采纳答案by ccKep
From the GNU C Library Manual:
来自 GNU C 库手册:
Function: char * fgets (char *s, int count, FILE *stream)
The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s, adding a null character to mark the end of the string. You must supply count characters worth of space in s, but the number of characters read is at most count ? 1. The extra character space is used to hold the null character at the end of the string.
功能:char * fgets (char *s, int count, FILE *stream)
fgets 函数从流中读取字符,直到并包括换行符,并将它们存储在字符串 s 中,添加一个空字符来标记字符串的结尾。您必须在 s 中提供 count 个字符的空间,但读取的字符数最多为 count ?1. 额外的字符空间用于保存字符串末尾的空字符。
So, fgets(key,1,stdin);
reads 0 characters and returns. (read: immediately)
因此,fgets(key,1,stdin);
读取 0 个字符并返回。(读:立即)
Use getchar
or getline
instead.
使用getchar
或getline
代替。
Edit: fgets also doesn't return once count
characters are available on the stream, it keeps waiting for a newline and then reads count
characters, so "any key" might not be the correct wording in this case then.
编辑:一旦count
流中的字符可用,fgets 也不会返回,它会一直等待换行符然后读取count
字符,因此在这种情况下,“任意键”可能不是正确的措辞。
You can use this exampleto avoid line-buffering:
您可以使用此示例来避免行缓冲:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int mygetch ( void )
{
int ch;
struct termios oldt, newt;
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int main()
{
printf("Press any key to continue.\n");
mygetch();
printf("Bye.\n");
}
回答by Matt
I'm not sure how to add a comment under a post so this isn't really an answer but a comment,
我不确定如何在帖子下添加评论,所以这不是真正的答案,而是评论,
In linux Stdin is buffered so you need to flush it which is what pressing 'enter' does on your terminal. It seems you want to read from an unbuffered stream i.e you want to react to a keypress imediately (without the need to explicitly flush it).
在 linux 中,Stdin 是缓冲的,因此您需要刷新它,这就是在终端上按“输入”的作用。似乎您想从无缓冲的流中读取数据,即您想立即对按键做出反应(无需显式刷新它)。
You can create your own unbuffered stream using a file discriptor and then read from it using "getc", you may have to use termios to setup unbuffered input as others have suggested.
您可以使用文件描述符创建自己的无缓冲流,然后使用“getc”从中读取,您可能必须像其他人建议的那样使用 termios 设置无缓冲输入。
回答by Hymanal
This is a simple method that worked for me on Windows 10:
这是一个在 Windows 10 上对我有用的简单方法:
#include <stdlib.h>
void pause(void);
int main(void)
{
printf("Testing pause.");
}
void pause(void)
{
system("pause");
}