我有兩個進程:和t2.cpp
。管道命令使標準輸入中斷
和t2.cpp
被簡化了,我想很容易地描述這個問題。
//t1.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hello\n"
<< "world\n"
<< "ok ok\n";
return 0;
}
//t2.cpp
#include <iostream>
#include <limits>
using namespace std;
int main()
{
string str;
while(getline(cin,str)){
cout << str <<endl;
}
//cin.clear();
//flush the cin
//cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
char x;
cin >> x;
return 0;
}
編譯和t2.cpp
後。我以這種方式執行它們./t1 | ./t2
。
發生問題! cin >> x;
in t2.cpp
失敗!我沒有機會從鍵盤上打字。
看起來pipe命令實現了redirecting the STDIN_FILENO
。它是否同時禁止標準輸入?
我的苛刻要求,是從t1
的輸出與shell命令|
獲取數據,此外,我想與用戶交互t2
。例如,我將顯示Sure to del?[y/n],and wait users's anwser.
不是真正的C++問題。 https://unix.stackexchange.com/questions/103885/piping-data-to-a-processs-stdin-without-causing-eof-afterward – LogicStuff
我認爲正確的答案是「do 「,但我不知道那是什麼。它是從'/ dev/tty'讀取的嗎?那麼,我怎麼能可靠地發送輸入到我管道'less'的任何命令呢? –