Results 1 to 11 of 11

Thread: File reading does not output properly

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default File reading does not output properly

    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:
    Qt Code:
    1. bool readFileInfo(QString fileName){
    2. QFile myFile(fileName);
    3. float x;
    4. float y;
    5. if(!myFile.exists()){
    6. qDebug()<<"The file"<<myFile.fileName()<<"does not exist.";
    7. return false;
    8. }
    9. if(!myFile.open(QIODevice::ReadOnly | QIODevice::Text)){
    10. qFatal("Could not open the file");
    11. return false;
    12. }
    13.  
    14. QTextStream st(&myFile);
    15.  
    16. while (!st.atEnd()){
    17. QString text;
    18. text=st.readLine();
    19. lineNumber++;
    20. if (text.contains("Polygon")){
    21. countBoundary++;
    22. lineNumberForBoundary.push_back(lineNumber);
    23. }
    24.  
    25. if (!text.contains("Polygon")||!text.contains(":")){
    26. st>>x>>y;
    27. qDebug() << x << y;
    28. }
    29. }
    30. myFile.close();
    31. return true;
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: File reading does not output properly

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: File reading does not output properly

    Thank you!!! I could get rid of '0 0' at the end, but how can I delete the others with Polygon 0/1??

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: File reading does not output properly

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: File reading does not output properly

    no. the first line always has ':' such as o: - first line represent of a letter of pointers.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: File reading does not output properly

    Qt Code:
    1. if (!text.contains("Polygon")||!text.contains(":")) {
    To copy to clipboard, switch view to plain text mode 
    Let's choose a line:
    Qt Code:
    1. Polygon 0/1
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by ChrisW67; 14th October 2011 at 01:31.

  7. #7
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: File reading does not output properly

    Thank you for replying. I changed code like following:
    Qt Code:
    1. if (text.contains(":")){}
    2. else if (text.contains("Polygon")){
    3. countBoundary++;
    4. lineNumberForBoundary.push_back(lineNumber);
    5. } else {
    6. st>>x>>y;
    7. qDebug() <<x<<y;
    8. }
    To copy to clipboard, switch view to plain text mode 
    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?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: File reading does not output properly

    Have you single stepped through the program? Which input lines are generating the zeroes?

  9. #9
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: File reading does not output properly

    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

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: File reading does not output properly

    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"

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: File reading does not output properly

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 18:55
  2. Replies: 10
    Last Post: 27th September 2010, 23:15
  3. Replies: 14
    Last Post: 20th March 2010, 19:43
  4. Replies: 3
    Last Post: 18th October 2007, 18:07
  5. radiobutton output file
    By nitriles in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 09:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.