gpt4 book ai didi

c - C 中的 ncurses 应用程序 - 读取标准输入

转载 作者:太空狗 更新时间:2023-10-29 11:16:00 25 4
gpt4 key购买 nike

我正在为操作系统学术类(class)编写 Linux 标准 less 命令的简化版本,我被允许使用 ncurses 来简化它。 “简化”意味着用户应该能够使用箭头、PgUp、PgDown ScrollView ,还可以使用 g 和 G 跳转命令。此外,程序应从文件(其名称作为参数给出)或标准输入流中读取输入,例如

echo "test" | ./less

从文件中读取很容易(在这种情况下没有问题),问题出现在我使用管道运算符时 - getch() 一直返回 -1 (EOF) 并且没有响应任何按键。我当前的代码如下所示:

char c;

while((c = fget(stdin)) != EOF)
{
// reading from stdin char by char, let's say
}

// enabling ncurses mode
initscr();
keypad(stdscr, TRUE);
noecho();
cbreak();

int ch;

while(true)
{
ch = getch();

switch(ch)
{
case KEY_LEFT:
...
}
}

...

我认为这是冲洗流的问题,但没有一个类似的解决方案对我有用。我从 stdin 读取所有数据,直到最后 (EOF),所以我不明白为什么 EOF 留在缓冲区中。

有什么解决方法吗?我浏览过类似的问题,但没有找到解决问题的线索。也许 ncurses 不是我任务的好解决方案?

最佳答案

问题解决了,我在进入ncurses模式之前加了一行:

...
freopen("/dev/tty", "rw", stdin);
// enabling ncurses mode
initscr();
...

关于c - C 中的 ncurses 应用程序 - 读取标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com