2

したがって、私のプログラムの特定の部分は次のようになります。

printf("Please input command:\n>");
While 1 {
    if ((int c = read(STDIN_FILENO, input, Buffer_size) == 0) {
        break;
    }
    rest of the program uses strtok to break the input 
    down and store in array. Then pass it to a function which checks for 
    various commands and prints whatever was the command 
    suppose to do or gives syntax error for incorrect commands

    printf(">"); //last line

}

したがって、次のようになります。

Please input command:
addperson Batman
>person added
blahblah
Error: incorrect syntax

何らかの理由で印刷されません: ">". また、その後何かを入力するたびに、正しいコマンドであっても常に同じことを言います。

しかし、これを使用すると:

printf("Please input command:\n>");
while 1 {
    if (fgets(input, Buffer_size, stdin) == NULL) {
        break;
    }
    ...
    printf(">");

}

私は得る:

Please input command:
> add_person Batman
person added
> blahbagwa
Incorrect syntax
> add_person Superman
person added

各出力に ">" がどのように表示されるかに注目してください。read が正しく機能しない理由はよくわかりません。おそらく私の読書の理解はあまり良くありません。誰にもアイデアはありますか?

4

1 に答える 1

-1

read()バッファ全体を満たすのに十分な入力を受け取るまでブロックし、 asは入力さfgets()れた各行のバッファを返します。

于 2013-04-05T01:26:08.613 に答える