残培和的putChar不工作没有回报 [英] getch and putchar not working without return
问题描述
我一直在试图让残培
来在没有成功的另一个程序中工作。所以,我做了我可以使用残培
我希望它在主程序中的工作方式最基本的程序。
我已经研究了 NOECHO
, CBREAK
, initscr的$ C的需要$ C>和
NODELAY
,我也尝试过使用 newscr()
但都没有成功。
我遇到的问题是,字符不会被显示在屏幕上,直到我打进的时候,他们应该是把
来每次循环屏幕。这究竟是为什么?也光标没有在新线路返回到屏幕的左侧。例如:
ABC
DEF
GHI
我看过的答案,但我再次难倒...
的#include<&stdio.h中GT;
#包括LT&;&ncurses.h GT;诠释的main()
{
initscr的(); CBREAK(); NOECHO(); NODELAY(stdscr上,0);
焦炭℃;
而((C =残培())!= EOF){
的putchar(三);} 返回0;
}
您没有看到输出,因为你的标准输出
流线缓冲。
您的程序越来越单个字符的所有权利;但输出流被缓冲。
尝试 fflush(标准输出);
或切换标准输出
来缓冲模式与则setbuf(标准输出,NULL);
与禁用缓冲的问题是,它是低效率的批量数据处理时的输出不是终端
您可以使它有条件的标准输出是一个TTY:
如果(isatty(的fileno(标准输出)))/ *#包括LT&;&unistd.h中GT; * /
则setbuf(标准输出,NULL);
要返回光标到行的开始,你需要拿出一个回车 \\ r
。这是因为诅咒 CBREAK
模式已停用 ONLCR
TTY模式(输出,发送时NL添加CR)。
如果您无条件地添加 \\ r
,那么当你的输出重定向它将出现在文件中。所以,再一次你需要一些 isatty
破解。
有一个更好的可能是学习如何使用 tcgetattr
和 tcsetattr
函数precisely控制具体参数TTY,如果你想要的是做字符在-A-时间输入没有回声,而不是实际开发交互式基于光标的计划。
你真的想要的字符在-A-时间输入,或者只是魔鬼的回声?这很容易禁止回声。呼叫 tcgetattr
来填补结构的termios
与文件描述符0的当前设置(如果它是一个TTY)。翻转一些标志关闭呼应,然后调用 tcsetattr
来安装更新的结构。当你的程序退出,是好的,放回到原来的。完成。
I have been trying to get getch
to work in another program with no success. So I have made the most basic program I can using getch
the way I want it to work in the main program.
I have researched the need for noecho
, cbreak
, initscr
and nodelay
, I have also tried using newscr()
but to no success.
The problem I am having is that the chars aren't being printed to the screen till I hit "enter", when they should be put
to the screen every loop. Why is this happening? Also the cursor doesn't return to the left of the screen at the new line. eg.
abc
def
ghi
I have looked for the answer but am stumped again...
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr();cbreak(); noecho();nodelay(stdscr,0);
char c ;
while((c=getch())!=EOF){
putchar(c);}
return 0;
}
You're not seeing the output because your stdout
stream is line buffered.
Your program is getting the individual characters all right; but the output stream is buffering them.
Try fflush(stdout);
or switching stdout
to unbuffered mode with setbuf(stdout, NULL);
.
The problem with disabling buffering is that it's inefficient for bulk data processing when the output isn't a terminal.
You can make it conditional on the standard output being a tty:
if (isatty(fileno(stdout))) /* #include <unistd.h> */
setbuf(stdout, NULL);
To return the cursor to the start of the line, you need to put out a carriage return \r
. This is because curses' cbreak
mode has disabled the ONLCR
tty mode (on Output, when sending NL add CR).
If you unconditionally add \r
, then it will appear in files when your output is redirected. So again you need some isatty
hack.
A much better might be to learn how to use the tcgetattr
and tcsetattr
functions to precisely control specific tty parameters, if all you want is to do character-at-a-time input without echo, and not actually develop an interactive curses-based program.
Do you really want character-at-a-time input, or just to diable echo? It's easy to disable echo. Call tcgetattr
to fill a struct termios
with the current settings of file descriptor 0 (if it is a tty). Flip some flags to turn off echoing, then call tcsetattr
to install the updated structure. When your program exits, be nice and put back the original one. Done.
这篇关于残培和的putChar不工作没有回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!