1
votes

I'm trying to make .obj parser for one project.

I have ObjLoader class with loaddata method. And it reads file line by line and parse it as I need.

QFile file(sourceFolder+ QString("/") +filename);
file.open(QIODevice::ReadOnly);
QTextStream textStream(&file);

QString currentString;

while (!textStream.atEnd())
    {
        currentString = textStream.readLine();
        //Some code here without closing file(I doublechecked this) 
    }
file.close()

It reads first 600 strings well, but then unexpectedly stops.
It happens on these strings:

v 3.956570 0.532126 0.300000
v 3.958734 0.593226 -0.300000
v 3.958731 0.593220 0.300000
vn 0.0000 -0.0000 1.0000
vn 0.0000 0.0000 -1.0000
vn 0.4852 -0.8744 0.0000

But when I tried

std::cerr "<<" currentString.toStdString() "<<" "\n";
(Sorry, don't know how to write this normally with stackoverflow formatting)

for these strings by using index "int i" which increments each time in the loop, it gives only this:

v 3.956570 0.532126 0.300000
v 3.958734 0.593226 -0.300000
v 3.958731 0.593220 0.300000
v

And in this point it stops reading file without any errors. It looks like the file in memory ends here. But it is only the 601-st string, when the file has 1100+ of them.

I checked the file in the text editor for unprintable symbols, but here is not any of them in this part.

Each time when I start debugging of this code it do the same thing - the same string stops the reading at the same symbol. Why can it happen?

1
What text encoding is the file? If you don't know, consider reading QByteArray from QFile intead of using QTextStream, because using wrong encoding can lead to funny things...hyde
ASCII text fileStakan Stakan

1 Answers

0
votes

Apparently textStream.atEnd() is not always a reliable indicator that you're actually at the end of a stream. I would try textStream.ReadAll() and see if you get the entire file that way. Alternatively, you can just call textStream.readLine() until it returns NULL and use that to break the while loop:

currentString = textStream.readLine();
while (!currentString.isNull())
    {
        //Some code here without closing file(I doublechecked this) 
        currentString = textStream.readLine();
    }