jrmrjnck
7th November 2010, 23:37
I have an odd problem where reading from stdin is always executing before writing to stdout (using QTextStream for both), regardless of their order in the code. This popped up in some code I was writing, but this reduced version below has the same problem:
#include <QTextStream>
int main( int argc, char* argv[] )
{
QTextStream cout( stdout, QIODevice::WriteOnly );
QTextStream cin( stdin, QIODevice::ReadOnly );
QString answer;
cout << "Answer: ";
cin >> answer;
return 0;
}
When it runs, it sits around waiting for input - only after I type something and press return does it output "Answer: ". I have tried many things to figure out what's happening:
if( argc == 2 ) {
cin >> answer;
}
doesn't fix it - it still asks for input first (unless argc != 2)
QString answer("wtf");
cout << "Answer: " << answer;
cin >> answer;
cout << endl << answer;
This is weird - it asks for the answer first, then outputs "Answer: wtf" followed by a newline and whatever input you gave it. Meaning that it reads stdin first, but it doesn't really store it in answer until it's supposed to. However, that's not entirely true:
QString answer("wtf");
cout << "Answer: " << answer;
cin >> answer;
if( answer == "asdf" ) {
cin >> answer;
}
cout << endl << answer;
This asks for input once and, only if you give it "asdf", asks a second time. Then it outputs "Answer: wtf" followed by a newline and whatever you told it the second time.
Building in debug, without optimizations, etc., makes no difference. This only happens on one computer/OS so I doubt it's reproducible for anyone else.
This is clearly not intended behavior and either a bug or configuration error on my part. I'm hesitant to install a different version of Qt as a few important things like KDE probably depend on it and it's currently working in all regards except for this odd problem. Using iostream is my workaround for the moment. Any suggestions?
Arch Linux x86_64 with KDE 5.4.2
Qt 4.7.0-4
gcc 4.5.1
#include <QTextStream>
int main( int argc, char* argv[] )
{
QTextStream cout( stdout, QIODevice::WriteOnly );
QTextStream cin( stdin, QIODevice::ReadOnly );
QString answer;
cout << "Answer: ";
cin >> answer;
return 0;
}
When it runs, it sits around waiting for input - only after I type something and press return does it output "Answer: ". I have tried many things to figure out what's happening:
if( argc == 2 ) {
cin >> answer;
}
doesn't fix it - it still asks for input first (unless argc != 2)
QString answer("wtf");
cout << "Answer: " << answer;
cin >> answer;
cout << endl << answer;
This is weird - it asks for the answer first, then outputs "Answer: wtf" followed by a newline and whatever input you gave it. Meaning that it reads stdin first, but it doesn't really store it in answer until it's supposed to. However, that's not entirely true:
QString answer("wtf");
cout << "Answer: " << answer;
cin >> answer;
if( answer == "asdf" ) {
cin >> answer;
}
cout << endl << answer;
This asks for input once and, only if you give it "asdf", asks a second time. Then it outputs "Answer: wtf" followed by a newline and whatever you told it the second time.
Building in debug, without optimizations, etc., makes no difference. This only happens on one computer/OS so I doubt it's reproducible for anyone else.
This is clearly not intended behavior and either a bug or configuration error on my part. I'm hesitant to install a different version of Qt as a few important things like KDE probably depend on it and it's currently working in all regards except for this odd problem. Using iostream is my workaround for the moment. Any suggestions?
Arch Linux x86_64 with KDE 5.4.2
Qt 4.7.0-4
gcc 4.5.1