#include <cstdio>
#include <QtCore/QProcess>
int main (int argc, char** argv) {
// if we remove 3 following lines, the problem described below doesn't exists!!
QProcess process;
process.start ("asdqwe"); // doesn't matter what we try to execute here.
process.waitForStarted (1000);
while (true) {
char buf[100];
if (scanf ("%s", buf) == EOF) { // it looks like stdin is closed!
printf("FAIL\n");
return 1;
}
printf ("%s\n", buf);
}
return 0;
}
このコードは、問題を示すための単なるスニペットです。完全なアプリケーションでは、プロセスとの読み取り/書き込み通信が必要です。
私はそれをコンパイルします:
g++ -o out ./main.cpp -I /usr/include/qt4/ -lQtCore
ターミナルの bash コマンドラインから実行します。
このプログラムが時々 FAIL を出力し、時々ループにとどまることがあるのはなぜですか?
編集: これは scan/printf に関する質問ではありません。同じ問題は、iostreams + 文字列を使用する場合です。この質問は、QProcess と親プロセスのファイル記述子との相互作用に関するものです。