Click here to Skip to main content
14,932,737 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.
   
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.
Jochen Arndt 30-Aug-18 3:23am
   
What is the input line?
What is the line count for that line?
What is in the splitted strings?
Member 13927363 30-Aug-18 3:32am
   
what it outputs is the content of the file using the code you provided.
Jochen Arndt 30-Aug-18 3:36am
   
I don't have your file so that I did not know what it is.

So either you are able to find the problem by inspecting the output (should be obvious when it is not splitted into 5 parts as expected) or post it here.
Member 13927363 30-Aug-18 3:42am
   
Hi, I have updated the codes. Hope you can help me figure out what seemed the problem. Thank you.
Member 13927363 30-Aug-18 3:44am
   
if you mean the file content:


NUMPOLLIES 36

// Floor 1
-3.0 0.0 -3.0 0.0 6.0
-3.0 0.0 3.0 0.0 0.0
3.0 0.0 3.0 6.0 0.0

-3.0 0.0 -3.0 0.0 6.0
3.0 0.0 -3.0 6.0 6.0
3.0 0.0 3.0 6.0 0.0

// Ceiling 1
-3.0 1.0 -3.0 0.0 6.0
-3.0 1.0 3.0 0.0 0.0
3.0 1.0 3.0 6.0 0.0
-3.0 1.0 -3.0 0.0 6.0
3.0 1.0 -3.0 6.0 6.0
3.0 1.0 3.0 6.0 0.0

// A1

-2.0 1.0 -2.0 0.0 1.0
-2.0 0.0 -2.0 0.0 0.0
-0.5 0.0 -2.0 1.5 0.0
-2.0 1.0 -2.0 0.0 1.0
-0.5 1.0 -2.0 1.5 1.0
-0.5 0.0 -2.0 1.5 0.0

// A2

2.0 1.0 -2.0 2.0 1.0
2.0 0.0 -2.0 2.0 0.0
0.5 0.0 -2.0 0.5 0.0
2.0 1.0 -2.0 2.0 1.0
0.5 1.0 -2.0 0.5 1.0
0.5 0.0 -2.0 0.5 0.0

// B1

-2.0 1.0 2.0 2.0 1.0
-2.0 0.0 2.0 2.0 0.0
-0.5 0.0 2.0 0.5 0.0
-2.0 1.0 2.0 2.0 1.0
-0.5 1.0 2.0 0.5 1.0
-0.5 0.0 2.0 0.5 0.0

// B2

2.0 1.0 2.0 2.0 1.0
2.0 0.0 2.0 2.0 0.0
0.5 0.0 2.0 0.5 0.0
2.0 1.0 2.0 2.0 1.0
0.5 1.0 2.0 0.5 1.0
0.5 0.0 2.0 0.5 0.0

// C1

-2.0 1.0 -2.0 0.0 1.0
-2.0 0.0 -2.0 0.0 0.0
-2.0 0.0 -0.5 1.5 0.0
-2.0 1.0 -2.0 0.0 1.0
-2.0 1.0 -0.5 1.5 1.0
-2.0 0.0 -0.5 1.5 0.0

// C2

-2.0 1.0 2.0 2.0 1.0
-2.0 0.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 0.0 0.0

// Left hallway - Hi

-3.0 1.0 -0.5 1.0 1.0
-3.0 0.0 -0.5 1.0 0.0
-2.0 0.0 -0.5 0.0 0.0
-3.0 1.0 -0.5 1.0 1.0
-2.0 1.0 -0.5 0.0 1.0
-2.0 0.0 -0.5 0.0 0.0

// Right hallway - Lw

3.0 1.0 0.5 1.0 1.0
3.0 0.0 0.5 1.0 0.0
2.0 0.0 0.5 0.0 0.0
3.0 1.0 0.5 1.0 1.0
2.0 1.0 0.5 0.0 1.0
2.0 0.0 0.5 0.0 0.0

// Right hallway - Hi

3.0 1.0 -0.5 1.0 1.0
3.0 0.0 -0.5 1.0 0.0
2.0 0.0 -0.5 0.0 0.0
3.0 1.0 -0.5 1.0 1.0
2.0 1.0 -0.5 0.0 1.0
2.0 0.0 -0.5 0.0 0.0
Jochen Arndt 30-Aug-18 4:28am
   
I mean what is read and what is expected.

Your file contains empty lines and lines containing not five floating point values. For those lines the splitted count might have of course different values. And the code checks if the words are floating point values.

My suggestion to report an error when the line count is not five was based on the assumption that the file contains only "valid" lines. Just not report that and all should be fine.

So what is your problem now?
From my point of view it should work as expected. That is: The data are appended to triangels.

Member 13927363 30-Aug-18 4:34am
   
the only problem here is the line QStringList line = QString::split( " ", ts.readLine().simplifyWhiteSpace() );, this syntax must help to get the 5 floating number to be store to line[] to be able to load a triangle, but I can't figure out how. by the way thank you so much for the effort.
Jochen Arndt 30-Aug-18 4:56am
   
From my point of view (I have not executed it), it is doing that. But with Qt5 you should use QString::simplified().

Because you have also lines containing not five floats, these must be skipped / ignored. But the following code does that.

If you don't know how it is working, print the intermediate results as suggested or inspect them with the debugger, and read and understand the documentation of the used functions.

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