簡體   English   中英

如何在 C 中正確使用 setvbuf?

[英]How to properly use setvbuf in C?

我想使用setvbuf()將我的輸入 stream 設置為無緩沖,但我真的不知道如何使用它。

我做了一些谷歌搜索,發現當您將 stream 設置為無緩沖時,緩沖區和大小參數將被忽略,如本文所述。

這是我的代碼:

#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';

        //...
    }
}

文章說它們被忽略了,但我仍然收到有關參數不足的錯誤,所以我不知道該放什么。 有人能幫助我嗎?

使用參數:

setvbuf(stdin, NULL, _IONBF, 0);

但是,這將不允許您在不按Enter的情況下從終端讀取字符,這是終端問題,而不是緩沖問題,因為您需要其他東西。

您可以使用ncurses ,這是建議的路線,但您也可以使用這篇文章中接受的答案What is the equivalent to getch() & getche() in Linux? 這使您可以做到這一點。

你可以看到它在這里工作: https://replit.com/@anastaciu/ConcernedAncientMenu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM