且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c - 从中​​读取标准输入的字符被键入

更新时间:2023-11-17 23:43:46

如果你真的想要的人物,因为他们输入,你不能用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;
}