0

I would like to flush my buffer after every three characters are typed in (instead of the \n). What would be the proper way to change the line-buffer trigger to be from being \n to being every 3 chars?

So far I have something like this:

#include<stdio.h>
#define CHAR_BUFFER 3

int main(void)
{

    int ch;
    int num=0;

    while ((ch = getchar()) != EOF) {
        if (ch == '\n') continue; // ignore counting newline as a character
        if (++num % CHAR_BUFFER == 0) {
            printf("Num: %d\n", num);
            fflush(stdout);
            putchar(ch);
            
        }
    }

    return 0;
}

What the program currently produces is:

$ main.c
Hello how are you?
Num: 3
lNum: 6
 Num: 9
wNum: 12
rNum: 15
yNum: 18
?

So instead of printing out all three chars, it seems to only grab the last one. What would be the correct way to do this?

Here are two examples of what I want:

H<enter>
// [nothing returned since we have not yet hit our 3rd character]
el<enter>
Hel // [return 'Hel' since this is now a multiple of three]
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

2

putchar() shouldn't be inside the if. You want to print all the characters, the condition is just for flushing.

#include<stdio.h>
#define CHAR_BUFFER 3

int main(void)
{

    int ch;
    int num=0;

    while ((ch = getchar()) != EOF) {
        if (ch == '\n') continue; // ignore counting newline as a character
        putchar(ch);
        if (++num % CHAR_BUFFER == 0) {
            printf("Num: %d\n", num);
            fflush(stdout);            
        }
    }

    return 0;
}

Note that this is for flushing the output buffer. It has nothing to do with how input is read or echoed, which requires using OS-specific functions.

See Capture characters from standard input without waiting for enter to be pressed and ANSI C No-echo keyboard input

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • that just prints everything for me though. For example, if I type in "H", it will print "H". I don't want it to do anything until I've hit every third character... – David542 Jan 26 '21 at 21:18
  • I updated the question with an example of example output. – David542 Jan 26 '21 at 21:20
  • Are you talking about the echoing of your input as you type it? That doesn't come from the program, that's part of the terminal driver. – Barmar Jan 26 '21 at 21:20
  • Are you typing just H or H ? – Barmar Jan 26 '21 at 21:21
  • If you don't want the Enter printed, move `putchar()` after the code that ignores newline. – Barmar Jan 26 '21 at 21:22
  • right, but if I type in `H`...that will still echo `H`...I don't want it to echo anything until the third character. – David542 Jan 26 '21 at 21:24
  • You can't control echoing with standard C, that requires OS-specific code. – Barmar Jan 26 '21 at 21:25
  • Oh, I see. I thought it was something that could be done by flushing the buffer manually. Would you want to write that in your answer and perhaps show an example of what cmd to use to do that and I'll accept your answer? Can it be done with `ioctl` ? – David542 Jan 26 '21 at 21:26
  • 1
    I've updated the answer with links to other questions that answer how to control input processing. – Barmar Jan 26 '21 at 21:29