PDA

View Full Version : Problem with QFile::atEnd() function



bbad68
11th February 2010, 01:20
Hi,

I'm trying to read from a .txt file using QTextStream.

Here's an idea of what I have in my code:


while(!file.atEnd())
{
//Read a line from the file
//Do something
}

The problem is that file.atEnd() returns 'true' after the loops is executed once, even when there is more than one line in the file. What is wrong?

PS: The .txt file that is read from is one that was generated in a different section of the same program as the above code. It contents are organized into columns, and when a new line is written in the file, the end of the old line is signified with the "\r\n" characters. (I really hope this makes sense :o).

franz
11th February 2010, 06:56
Can you post a bit more code with what you are actually doing in that loop?

JD2000
11th February 2010, 12:47
Try this:


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

while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}

The above was lifted straight out of the documentation, QIODevice::Text is needed to convert the line end markers from '\r\n' and is probably the source of your problem.

bbad68
11th February 2010, 18:26
The above was lifted straight out of the documentation, QIODevice::Text is needed to convert the line end markers from '\r\n' and is probably the source of your problem.

Hmmm, still doesn't seem to work.


Can you post a bit more code with what you are actually doing in that loop?

Sorry, I probably should've done this in the first place :P

Here is the code for WRITING the file:


QFile file(fileName);

if(!file.open(QIODevice::WriteOnly)){
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}

QTextStream out(&file);
out << /*column headers*/ << \r\n;

for(int i = 0; i < nLines; i++)
{
out << /*column values*/ << \r\n;
}

Here's a better look at the code for READING from the file:


QFile file(fileName);

if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}

//I used the approach in lines 12-15 to read the column headers
//because QTextStream::readLine() also made file.atEnd() return
//true, so the while loop was being skipped entirely
char firstLineIterator = 0;
while(firstLineIterator!= 10)
file.getChar(&firstLineIterator);
QTextStream in(&file);

while(!file.atEnd())
{

//Store file name and direction (The first 2 values in each line in the
//file are strings, the rest are doubles)
QString fileName, direction;
in >> fileName >> direction;

//Store double values for the line
double inputArray[32];
for(int i=0; i<32; i++)
in >> inputArray[i];

/*store array values into spin boxes on the form*/

}

I hope this is enough for you to get a better idea of what the problem could be :)

pitonyak
11th February 2010, 21:29
This will not always work correctly in special cases, but I will not begin to guess if this is your problem.


Do you close your file after writing, but before reading?
Have you looked at the file to verify that you have the expected carriage returns and line feeds? (may differ from platform to platform)
Do you run out of data after the first read?
Have you verified that while reading one line, you really do read exactly what you expect on every line until it signals the end?

norobro
11th February 2010, 21:32
You're operating on a stream not the file. Try using QTextStream::atEnd() instead.