#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 + string을 사용하는 경우입니다. 이 질문은 QProcess와 상위 프로세스의 파일 설명 자의 상호 작용에 관한 것입니다.
모두 사용했다 코드가 표시됩니다. 가져 가서 컴파일하고 실행하십시오. –
... 그리고 당신은 이상한 행동을 보게 될 것입니다 (적어도 리눅스에서는) –
include에 실수가있었습니다. 결정된. –