PDA

View Full Version : QFile not working with >>



timmu
30th December 2011, 06:21
Hi,

I'm trying to use QFile to open a text file (floating numbers in a single column) and read the numbers. The example below won't even compile, something is wrong with it but I don't know what.



QFile INfile("example1.txt");
if(!INfile.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
float numb;

while (!INfile.atEnd())
{
INfile >> numb;
cout << numb << "\n";
}

INfile.close();


Thanks

stampede
30th December 2011, 06:37
Use QTextStream to read from text files:


QFile INfile("example1.txt");
if(!INfile.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
float numb;
QTextStream stream(&INfile);
while (!stream.atEnd())
{
stream >> numb;
cout << numb << "\n";
}
INfile.close();

amleto
30th December 2011, 13:13
op, docs are here:
http://developer.qt.nokia.com/doc/qt-4.8/qfile.html

and they don't mention anything about operator >>.

QTextStream on the other hand...
http://developer.qt.nokia.com/doc/qt-4.8/qtextstream.html


The Qt docs are very good. Your compiler, I'm sure, will have given a quite obvious error. Part of being a programmer is being able to solve small issues like this. If you put no effort into learning how to resolve these trivial issues, you will find yourself posting, and waiting, and wasting lots and lots of time.