PDA

View Full Version : File reading does not output properly



sydjung
13th October 2011, 23:44
Hi guys, I am trying to read a text file that represent polygon pointers.
example text file format is like following:
o:
Polygon 0/1
704 -1296.625
961.8125 -1242.313
1163.25 -1079.375
1273.594 -880
1310.375 -653.125
Polygon 1/1
661.375 -1207.25
588.3281 -1196.422
514.9375 -1163.938
448.4219 -1104.297
396 -1012
361.9688 -887.9063

Basically, my function reads each line and each line has two numbers. Then I read those two numbers separately in x,y variable. Finally, it displays only those x, y numbers. But I get output like following:
0 0
704 -1296.625
961.8125 -1242.313
1163.25 -1079.375
1273.594 -880
1310.375 -653.125
0 0
661.375 -1207.25
588.3281 -1196.422
514.9375 -1163.938
448.4219 -1104.297
396 -1012
361.9688 -887.9063
0 0
I don't know where all those '0 0' come from. does anyone know what is the cause?
Here is my code:


bool readFileInfo(QString fileName){
QFile myFile(fileName);
float x;
float y;
if(!myFile.exists()){
qDebug()<<"The file"<<myFile.fileName()<<"does not exist.";
return false;
}
if(!myFile.open(QIODevice::ReadOnly | QIODevice::Text)){
qFatal("Could not open the file");
return false;
}

QTextStream st(&myFile);

while (!st.atEnd()){
QString text;
text=st.readLine();
lineNumber++;
if (text.contains("Polygon")){
countBoundary++;
lineNumberForBoundary.push_back(lineNumber);
}

if (!text.contains("Polygon")||!text.contains(":")){
st>>x>>y;
qDebug() << x << y;
}
}
myFile.close();
return true;
}

wysota
13th October 2011, 23:54
They come from the fact that "Polygon 0/1" doesn't represent two numbers (nor does "Polygon 1/1" and nor does the end of line character/empty line/lots of spaces at the end of file).

sydjung
14th October 2011, 00:10
Thank you!!! I could get rid of '0 0' at the end, but how can I delete the others with Polygon 0/1??

wysota
14th October 2011, 00:19
Apparently the problem is with line #25 of your code which triggers it for every line because the condition is always true in case of the sample provided (since no line contains ":", !contains(":") is always true).

sydjung
14th October 2011, 00:27
no. the first line always has ':' such as o: - first line represent of a letter of pointers.

ChrisW67
14th October 2011, 01:22
if (!text.contains("Polygon")||!text.contains(":")) {

Let's choose a line:


Polygon 0/1

The first part of the "or" expression: false because Polygon is present
The second part of the or expression: true because the line does not contain ":"
The result: true

"NOT(A OR B)" is not the same thing as "NOT A OR NOT B". Read about De Morgan's Laws (http://en.wikipedia.org/wiki/De_Morgan%27s_laws)

sydjung
14th October 2011, 01:57
Thank you for replying. I changed code like following:


if (text.contains(":")){}
else if (text.contains("Polygon")){
countBoundary++;
lineNumberForBoundary.push_back(lineNumber);
} else {
st>>x>>y;
qDebug() <<x<<y;
}

but the output it displays me was weird. Here is the output.
961.8125 -1242.313
1163.25 -1079.375
1273.594 -880
1310.375 -653.125
0 0
588.3281 -1196.422
514.9375 -1163.938
448.4219 -1104.297
396 -1012
361.9688 -887.9063

they miss the line after Polygon 0/1 and Polygon 1/1

I think there's something wrong when I read x,y value using st>>x>>y
Any Idea?

ChrisW67
14th October 2011, 02:14
Have you single stepped through the program? Which input lines are generating the zeroes?

sydjung
14th October 2011, 02:18
i have zeroes when i test the line with 'Polygon 0/1 and Polygon 1/1'. when i checked with exact string like Polygon 0/1 or Polygon 1/1, it still give me zeroes for those lines

ChrisW67
14th October 2011, 03:03
I think you need to single step and pay more attention to which input lines end up in variable text.

Here is a clue: at line six you are reading another line from the stream.
Another: In your example output the first Polygon is not generating "0 0"

wysota
14th October 2011, 10:04
I think relying on the presence of "Polygon" in the line is a bad approach. I think you the test should be "does the line contain something else than two numbers?" You can use regular expressions for that.