2

I would like to set my input stream as unbuffered using setvbuf() but I don't really know how to use it.

I've done some googling and found that when you set a stream to unbuffered, the buffer and size parameters are ignored, as said by this article.

Here is my code:

#include "structures.h"
#include "functions.h"

char choice;
int option;

int main(void) {
    while(1) {
        puts("============================================Select an Option============================================");
        puts("1. Create Linked List");
        puts("2. Display items");
        puts("3. Add item to the beginning");
        puts("4. Add item to the end");
        puts("5. Add item in the middle");
        puts("6. Delete item from the beginning");
        puts("7. Delete item from the end");
        puts("8. Delete item from the middle");
        puts("9. Exit");
        printf(">>> ");

        setvbuf(stdin, _IONBF);
        choice = getchar();
        cleanStdin();
        option = choice - '0';

        //...
    }
}

The article says they're ignored but I still get an error about not enough parameters, so I don't know what to put in there. Can someone help me please?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Matthew Schell
  • 599
  • 1
  • 6
  • 19

1 Answers1

2

Use fake parameters:

setvbuf(stdin, NULL, _IONBF, 0);

This, though, will not allow you to read charaters from the terminal without pressing Enter, this a terminal problem, not a buffering one, for that you need something else.

You can you use ncurses, which is the advised route, but you can also use the accepted answer in this post What is the equivalent to getch() & getche() in Linux? which allows you to do exactly that.

You can see it working here: https://replit.com/@anastaciu/ConcernedAncientMenu

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Thank you for helping but for some reason, I still have to press enter to send the characters to stdin. If I'm not mistaken, unbuffered input means that input is sent to the program right when I press a key right? Or is there something else wrong? – Matthew Schell Apr 22 '21 at 14:37
  • @MatthewSchell, that would be nice, but no, you still have to press enter. – anastaciu Apr 22 '21 at 14:41
  • 1
    @MatthewSchell, the *problem* is in the terminal, not the buffering, you need to use something else, if your in a linux machine I would advise ncurses. – anastaciu Apr 22 '21 at 14:44
  • so something like `getch()`? – Matthew Schell Apr 22 '21 at 14:45
  • @MatthewSchell, yes, that's from windows `conio.h` isn't it? – anastaciu Apr 22 '21 at 14:46
  • Well I'm on a Mac so it would be ncurses.h for me – Matthew Schell Apr 22 '21 at 14:47
  • @MatthewSchell, yes, you'll find that it's very easy to use once you get used to it, maybe there are other ways, check this post https://stackoverflow.com/q/7469139/6865932, it's quite old, so some deprecation might be going on there, but it's worth checking. – anastaciu Apr 22 '21 at 14:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231468/discussion-between-matthew-schell-and-anastaciu). – Matthew Schell Apr 22 '21 at 14:51
  • @MatthewSchell the accepted answer in the link works perfectly, did you try it? I left a live demo in my answer. – anastaciu Apr 22 '21 at 21:00
  • for some reason it works in repl but not in CLion, I'll have to investigate that but I'll mark it as accepted anyway. Thanks! – Matthew Schell May 03 '21 at 03:31
  • @MatthewSchell That's indeed weird, try running it on a separate terminal window, IDE's sometimes don't alllow these to work properly – anastaciu May 03 '21 at 08:03
  • Doesn't work on a different terminal window either, except now i can't delete the first character i input. – Matthew Schell May 03 '21 at 10:51
  • IDK man, it seems that it's a problem local to your environment, try this test routine https://stackoverflow.com/a/1513806/6865932, see if it works, otherwise, maybe going for ncurses can be a easier way out. – anastaciu May 03 '21 at 11:15