![](/img/trans.png)
[英]Why does my command read with fgets() not work with execlp(), but the same command hard-coded works correctly?
[英]Why does read not work yet fgets work fine in my program?
因此,程序的特定部分如下所示:
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()
將阻塞,直到它收到足夠的輸入以填滿整個緩沖區為止,而fgets()
將為每個輸入的行返回一個緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.