Can't ouput charcter until newline in C 04-24-2017, 06:53 AM
#1
So... I was bored and working on my text editor project, when I found out I can't output characters read from non-canonical mode input right away, they will only output after a newline is made.
EX:
Neither printf or putchar will output anything until the enter key is hit. However, printf("%c\n", x) works fine.
Anybody wanna help me with this?
EX:
Code:
#define CTRL(key) ((key) & 0x1f)
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
struct termios orig_termios;
void rawOff() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void rawOn() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(rawOff);
struct termios raw = orig_termios;
raw.c_iflag &= ~(IXON);
raw.c_lflag &= ~(ICANON | ECHO | IEXTEN);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int main() {
rawOn();
while (1) {
char x;
read(STDIN_FILENO, &x, 1);
//printf("%c", x);
putchar(x);
if (x == CTRL('q')) {
return(0);
}
}
}
Neither printf or putchar will output anything until the enter key is hit. However, printf("%c\n", x) works fine.
Anybody wanna help me with this?
(This post was last modified: 04-24-2017, 06:54 AM by Blink.)