Problem with QFile::atEnd() function
Hi,
I'm trying to read from a .txt file using QTextStream.
Here's an idea of what I have in my code:
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).
Re: Problem with QFile::atEnd() function
Can you post a bit more code with what you are actually doing in that loop?
Re: Problem with QFile::atEnd() function
Try this:
Code:
return;
while (!file.atEnd()) {
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.
Re: Problem with QFile::atEnd() function
Quote:
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.
Quote:
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:
Code:
QMessageBox::information(this, tr
("Unable to open file"),
file.errorString());
return;
}
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:
Code:
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);
while(!file.atEnd())
{
//Store file name and direction (The first 2 values in each line in the
//file are strings, the rest are doubles)
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 :)
Re: Problem with QFile::atEnd() function
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?
Re: Problem with QFile::atEnd() function
You're operating on a stream not the file. Try using QTextStream::atEnd() instead.