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()将阻塞,直到它收到足够的输入来填充整个缓冲区,其中fgets()将为每个输入的行返回一个缓冲区。

于 2013-04-05T01:26:08.613 回答