PDA

View Full Version : Qt Prog skip the file reading block



dgg32
10th December 2008, 15:59
Hi, I am a new new Qt student. haven't quite captured Qt's feature. First I say thank you all for your viewing this post.

Today I have written such codes:



#include <QtGui>
#include <iostream>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QFile file("text.txt");
file.open(QIODevice::ReadOnly);
QTextStream in(&file);

std::cout << "Before the while loop." << '\n';

QString Line;
while (! in.atEnd())
{
std::cout << "a Line" << std::endl;
Line = in.readLine();

}
std::cout << "Finished the loop" << std::endl;

file.close();
return app.exec();
}


the text.txt has four lines.

So I am hopefully expecting the console output like:

Before the while loop.
a Line
a Line
a Line
a Line
Finished the loop

But after I went into the release folder and type the program name, the result was disappointing:
Before the while loop.
Finished the loop

Something is more strange, I use Code::Blocks. So if I compiled the program there and then hit its run button, the result was right, four "a Line" with the both head and tail statements.

And I haven't forgotten added in the .pro file such a line
CONFIG += console

So my question are:
What is wrong with my program? why make.exe -> in console type the program doesn't show the four "a line"? But Code::Blocks could build the program correctlly and run it reasonally?

Thank you all!!!

spirit
10th December 2008, 16:28
first of all if don't need event processing you don't need to create QApplication.
second, try to use next snippet of code


...
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
...
}
...

btw, this code from the Assistant, see QFile

dgg32
10th December 2008, 16:37
Hi thanks for your reply. I tried your snippet just now. The .exe in the release folder showed nothing, while the .exe in debug showed just like before: before and finished the loop. I don't have any clue.

spirit
10th December 2008, 17:07
put your txt with exe file or specify full path to file.

dgg32
10th December 2008, 17:34
put your txt with exe file or specify full path to file.

have already done those before came here.

spirit
10th December 2008, 17:36
what does QFile::open return?

dgg32
10th December 2008, 17:38
what does QFile::open return?

it returned true.

spirit
10th December 2008, 17:48
show your pro-file.

dgg32
10th December 2008, 17:51
show your pro-file.

################################################## ####################
# Automatically generated by qmake (2.01a) Mi 10. Dez 14:25:13 2008
################################################## ####################

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
CONFIG += console

# Input
SOURCES += main.cpp

spirit
10th December 2008, 18:02
so, looks like problem with determination of file path. try to set full path to file, i.e.
"c:/text.txt"

dgg32
10th December 2008, 18:12
so, looks like problem with determination of file path. try to set full path to file, i.e.
"c:/text.txt"

Man, Qt rocks, you rocks