Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
void loadTriangles()
{
QFile f( "C:Test/world.txt" );


if( !f.open( QIODevice::ReadOnly ) )
{
if( !QFile::exists(f.fileName()) )
{
QMessageBox::information(this, "Error",
QString("File %1 does not exist").arg(f.fileName()));
}
else
{
QMessageBox::information(this, "Error",
QString("Failed to open file %1: Error %2").arg(f.fileName(), f.error()));
}
}
else
{
// Read from file
QTextStream ts( &f );

Vertex v[3];
int vcount = 0;
bool allok, ok;

while( !ts.atEnd() )
{

QString curLine = ts.readLine();
qDebug() << curLine;
curLine = curLine.simplified();
qDebug() << curLine;
QStringList line = curLine.split(' ');
for (int i = 0; i < line.count(); i++)
qDebug() << line[i];


if( line.count() == 5 )
{
allok = true;
v[vcount].x = line[0].toFloat( &ok );
allok &= ok;
v[vcount].y = line[1].toFloat( &ok );
allok &= ok;
v[vcount].z = line[2].toFloat( &ok );
allok &= ok;
v[vcount].u = line[3].toFloat( &ok );
allok &= ok;
v[vcount].v = line[4].toFloat( &ok );
allok &= ok;

if( allok )
vcount++;

if( vcount == 3 )
{
vcount = 0;
Triangle t;
t.vertex[0] = v[0];
t.vertex[1] = v[1];
t.vertex[2] = v[2];

triangles.append( t );
}
}
else
{
QMessageBox::information(this, "Error", "Error here");
}
}

f.close();
}

}

What I have tried:

The error exists when the line.count() != 5

what it need is to convert
QStringList line = QString::split( " ", ts.readLine().simplifyWhiteSpace() );
to Qt5 compatibility so that each data from the file per line will be stored to line[] since each line consists of 5 data so it must line.count() == 5
Posted
Updated 29-Aug-18 22:29pm
v7

1 solution

It is not really clear what is not working as expected. But the most probable reason is that the file can't be found because you are using a plain file name without path. Then the file must be located in the current working directory. That is in most - but not all - cases the directory where the executable file is located.

I suggest to specify the full path to the file.

To know what went wrong you can add a check if the file exists:
if( !f.open( QIODevice::ReadOnly ) )
{
    if( !QFile::exists(f.fileName()) )
    {
        QMessageBox::information(this, "Error", 
            QString("File %1 does not exist").arg(f.fileName()));
    }
    else
    {
        QMessageBox::information(this, "Error", 
            QString("Failed to open file %1: Error %2").arg(f.fileName(), f.error()));
    }
}
else
{
    // Read from file

    f.close();
}
Note also that the above calls open() only once. In your posted code it is called twice and the second one is never closed when it succeeds.
 
Share this answer
 
v2
Comments
Member 13927363 29-Aug-18 4:38am    
this line gives error --- QMessageBox::information(this, QString("Error", "File %1 does not exist").arg(f.fileName())); --- so I changed to simple QMessageBox syntax, but none of the error exists but still no file was loaded.

Still can't figure out why the file cannot be loaded though I've changed its path.
Jochen Arndt 29-Aug-18 5:20am    
Uups. The "Error" string parameter belongs to the message box. Fixed.

What does "was not loaded" mean.
Is the file opened?
If not, it should be catched and reported by my solution.
If yes, it is related to processing the file content. If the file does not contain lines with 5 space separated floating point values, nothing will happen. You can check that by reporting this (when line.count() != 5) or debugging the application (inspecting values when stepping through the code or printing the values using QDebug).
Member 13927363 29-Aug-18 20:33pm    
tried when line.count() != 5 and there the error exist, maybe this line QStringList line = ts.readLine().simplified().split(" "); doesn't work. Would you please help me convert this QStringList line = QString::split( " ", ts.readLine().simplifyWhiteSpace() ); to Qt5 compatibilty format?
Jochen Arndt 30-Aug-18 3:05am    
By just reading it here it looks fine provided that the file content is in the expected format.

Split it into single function calls and inspect the content:
QString curLine = ts.readLine();
qDebug << curLine;
curLine = curLine.simplified();
qDebug << curLine;
QStringList line = curLine.split(' ');
for (int i = 0; i < line.count(); i++)
 qDebug << line[i];
Member 13927363 30-Aug-18 3:14am    
it read the line using the qDebug but still the line.count != 5, it should be == 5 to be able to load the file. I'm confused what seemed to be the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900