I am learning how to use QProcess to communicate with cmd.exe on WinXP. I wrote the following code, to send and receive messages and commands from my app to cmd.exe. This is a console application, not useful at all I know. I just wrote the simplier code possible to show you the error:
Code:
#include <QByteArray> #include <QProcess> #include <iostream> #include <string> using namespace std; int main(int argc,char** argv) { QProcess cmd; cmd.start("cmd"); if (!cmd.waitForStarted()) return false; cmd.waitForReadyRead(); cout << result.data(); string str; getline(cin,str); while(str != string("exit")) { cmd.write(str.c_str()); cmd.write("\n"); cmd.waitForReadyRead(); result = cmd.readAll(); cout << result.data(); getline(cin,str); } }
Everything works well, except that the commands I send are delayed. For instance:
1) I launch my console application, this appears:
2) I write "help" then press enter. Now the prompt looks like that:Quote:
Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
[path_to_my_current_dir]>
3) Then I press enter again, and my prompt displays:Quote:
Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
[path_to_my_current_dir]>
help
It prints the cmd help, but only when I press enter twice. I can't figure out why there is this delay.Quote:
Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
[path_to_my_current_dir]>
help
Pour plus d'informations sur une commande spécifique, entrez le nom de la commande HELP.
ASSOC Affiche ou modifie les applications associées aux extensions de...
When I press enter my program is at the line 24 and wait to be able to read the cmd.exe output (cmd.waitForReadyRead();). When it is possible, result takes this output and prints it to my console application. I don't understand why result prints "help" and not the actual help of cmd.
Thanks in advance I hope I was clear ;)