Results 1 to 6 of 6

Thread: Problem with QFile::atEnd() function

  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QFile::atEnd() function

    Hi,

    I'm trying to read from a .txt file using QTextStream.

    Here's an idea of what I have in my code:

    Qt Code:
    1. while(!file.atEnd())
    2. {
    3. //Read a line from the file
    4. //Do something
    5. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that file.atEnd() returns 'true' after the loops is executed once, even when there is more than one line in the file. What is wrong?

    PS: The .txt file that is read from is one that was generated in a different section of the same program as the above code. It contents are organized into columns, and when a new line is written in the file, the end of the old line is signified with the "\r\n" characters. (I really hope this makes sense ).

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QFile::atEnd() function

    Can you post a bit more code with what you are actually doing in that loop?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QFile::atEnd() function

    Try this:

    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4.  
    5. while (!file.atEnd()) {
    6. QByteArray line = file.readLine();
    7. process_line(line);
    8. }
    To copy to clipboard, switch view to plain text mode 

    The above was lifted straight out of the documentation, QIODevice::Text is needed to convert the line end markers from '\r\n' and is probably the source of your problem.
    Last edited by JD2000; 11th February 2010 at 12:58.

  4. #4
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QFile::atEnd() function

    The above was lifted straight out of the documentation, QIODevice::Text is needed to convert the line end markers from '\r\n' and is probably the source of your problem.
    Hmmm, still doesn't seem to work.

    Can you post a bit more code with what you are actually doing in that loop?
    Sorry, I probably should've done this in the first place :P

    Here is the code for WRITING the file:

    Qt Code:
    1. QFile file(fileName);
    2.  
    3. if(!file.open(QIODevice::WriteOnly)){
    4. QMessageBox::information(this, tr("Unable to open file"),
    5. file.errorString());
    6. return;
    7. }
    8.  
    9. QTextStream out(&file);
    10. out << /*column headers*/ << \r\n;
    11.  
    12. for(int i = 0; i < nLines; i++)
    13. {
    14. out << /*column values*/ << \r\n;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Here's a better look at the code for READING from the file:

    Qt Code:
    1. QFile file(fileName);
    2.  
    3. if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    4. QMessageBox::information(this, tr("Unable to open file"),
    5. file.errorString());
    6. return;
    7. }
    8.  
    9. //I used the approach in lines 12-15 to read the column headers
    10. //because QTextStream::readLine() also made file.atEnd() return
    11. //true, so the while loop was being skipped entirely
    12. char firstLineIterator = 0;
    13. while(firstLineIterator!= 10)
    14. file.getChar(&firstLineIterator);
    15. QTextStream in(&file);
    16.  
    17. while(!file.atEnd())
    18. {
    19.  
    20. //Store file name and direction (The first 2 values in each line in the
    21. //file are strings, the rest are doubles)
    22. QString fileName, direction;
    23. in >> fileName >> direction;
    24.  
    25. //Store double values for the line
    26. double inputArray[32];
    27. for(int i=0; i<32; i++)
    28. in >> inputArray[i];
    29.  
    30. /*store array values into spin boxes on the form*/
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    I hope this is enough for you to get a better idea of what the problem could be

  5. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QFile::atEnd() function

    This will not always work correctly in special cases, but I will not begin to guess if this is your problem.

    1. Do you close your file after writing, but before reading?
    2. Have you looked at the file to verify that you have the expected carriage returns and line feeds? (may differ from platform to platform)
    3. Do you run out of data after the first read?
    4. Have you verified that while reading one line, you really do read exactly what you expect on every line until it signals the end?

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QFile::atEnd() function

    You're operating on a stream not the file. Try using QTextStream::atEnd() instead.

  7. The following user says thank you to norobro for this useful post:

    bbad68 (11th February 2010)

Similar Threads

  1. Replies: 0
    Last Post: 4th November 2009, 10:21
  2. Problem with QFile and copying;
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 31st October 2009, 16:32
  3. QFile::atEnd() problem?
    By lvi in forum Qt Programming
    Replies: 9
    Last Post: 6th August 2008, 12:37
  4. Problem : use QHttp get a file to QFile
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 24th May 2007, 10:58
  5. QTextStream::atEnd()
    By blue.death in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2006, 19:30

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.