Sinisterly
Can't ouput charcter until newline in C - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: Can't ouput charcter until newline in C (/Thread-Can-t-ouput-charcter-until-newline-in-C)



Can't ouput charcter until newline in C - Blink - 04-24-2017

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?


RE: Can't ouput charcter until newline in C - Blink - 04-24-2017

Fixed by adding
Code:
setvbuf(stdout, 0, _IONBF, 0);

Closing thread.