c - 从中读取标准输入的字符被键入 [英] C - Reading from stdin as characters are typed
本文介绍了c - 从中读取标准输入的字符被键入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何用字符填写一个80字符缓冲区,因为他们正在进入或直到回车键为pressed,或缓冲区已满,以先到为准。
How do I fill an 80-character buffer with characters as they are being entered or until the carriage return key is pressed, or the buffer is full, whichever occurs first.
我已经研究过很多不同的方式,但进入已被pressed则输入字符*得到80切断。
I've looked into a lot of different ways, but enter has to be pressed then the input char* gets cut off at 80..
感谢。
推荐答案
如果你真的想要的人物,因为他们输入,你不能用C IO。你必须做它的UNIX方式。 (或窗口方式)
If you really want the characters "as they are entered", you cannot use C io. You have to do it the unix way. (or windows way)
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int main() {
char r[81];
int i;
struct termios old,new;
char c;
tcgetattr(0,&old);
new = old;
new.c_lflag&=~ICANON;
tcsetattr(0,TCSANOW,&new);
i = 0;
while (read(0,&c,1) && c!='\n' && i < 80) r[i++] = c;
r[i] = 0;
tcsetattr(0,TCSANOW,&old);
printf("Entered <%s>\n",r);
return 0;
}
这篇关于c - 从中读取标准输入的字符被键入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文