|
发表于 2022-4-12 23:58:22
|
显示全部楼层
setvbuf 这个函数还挺有意思的
下面这个代码是行缓存的(强制设置成行缓存),依然是 输入一个字符就读取一个字符,就显示一个字符
意思就是无法设置成行缓存模式
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <termios.h>
- int main(void) {
- struct termios termios;
- ioctl(STDIN_FILENO, TCGETS, &termios);
- termios.c_lflag &= ~ICANON;
- ioctl(STDIN_FILENO, TCSETS, &termios);
- setvbuf(stdin, NULL, _IOLBF, 1024);
- int ch;
- while((ch = getchar()) != 'q') putchar(ch);
- return 0;
- }
复制代码
下面这个代码设置成全缓存的
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <termios.h>
- int main(void) {
- struct termios termios;
- ioctl(STDIN_FILENO, TCGETS, &termios);
- termios.c_lflag &= ~ICANON;
- ioctl(STDIN_FILENO, TCSETS, &termios);
- setvbuf(stdin, NULL, _IOFBF, 1024);
- int ch;
- while((ch = getchar()) != 'q') putchar(ch);
- return 0;
- }
复制代码
结果真就全缓存了(大概)
因为输入一个字符,不在输出一个字符
但是输入字符q,程序就直接结束了,并不需要按下回车键了
代码改成这样,程序的行为和上面一个代码一样,所以 “结果真就全缓存了(大概)” 这个“大概”可以去掉了,(大概)
^_^
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <termios.h>
- int main(void) {
- /*
- struct termios termios;
- ioctl(STDIN_FILENO, TCGETS, &termios);
- termios.c_lflag &= ~ICANON;
- ioctl(STDIN_FILENO, TCSETS, &termios);
- */
- setvbuf(stdin, NULL, _IOFBF, 1024);
- int ch;
- while((ch = getchar()) != 'q') putchar(ch);
- return 0;
- }
复制代码
不玩了,到此为止
|
|