Login Register


Can't ouput charcter until newline in C filter_list
Author
Message
Can't ouput charcter until newline in C #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:
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.)


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.


RE: Can't ouput charcter until newline in C #2
Fixed by adding
Code:
setvbuf(stdout, 0, _IONBF, 0);

Closing thread.
(This post was last modified: 05-04-2017, 05:07 AM by Blink.)


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.








Users browsing this thread: 1 Guest(s)