Results 1 to 10 of 10

Thread: QFile::atEnd() problem?

  1. #1
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QFile::atEnd() problem?

    I have a problem with QFile::atEnd(). I have the following code (simplified):
    Qt Code:
    1. while (!partFile->atEnd()) {
    2. line = partFile->readLine();
    3. /* Do stuff with "line" */
    4. }
    To copy to clipboard, switch view to plain text mode 
    This works fine on Linux and on MacOS X. But on Windows, this results in an infinite loop, i.e. atEnd() never returns true. Of course, the file exists and I can read from it.

    I have noticed more oddities in QFile across platforms ("\" vs. "/" inconsistincies), but this one is kind of hard to work around...
    Any ideas on what causes this and/or how to fix it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QFile::atEnd() problem?

    atEnd() works fine for me on Windows, your problem has to be related to something else... maybe readLine() doesn't read the line? Does the file contain proper line endings for the platform (\r\n in case of Windows)?

  3. #3
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::atEnd() problem?

    readLine() is working fine: I get the data from the file. So I'd think that the line terminators are fine. Does atEnd() check for EOF? I can't understand why the end of file wouldn't be detected...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QFile::atEnd() problem?

    Try with different files. Or make a simple test:
    Qt Code:
    1. bool t1 = file.atEnd();
    2. qDebug() << t1;
    3. file.readAll();
    4. t1 = file.atEnd();
    5. qDebug() << t1;
    To copy to clipboard, switch view to plain text mode 
    Second debug statement should display true.

    Your current code is correct only when a file ends with an empty line, otherwise you won't be reading the last line.

  5. #5
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::atEnd() problem?

    I'm not sure if this result is relevant:

    Qt Code:
    1. while (partFile->canReadLine()) {
    2. line = partFile->readLine();
    3. /* Do stuff with "line" */
    4. }
    To copy to clipboard, switch view to plain text mode 
    Now the loop is not entered at all, i.e. partFile->canReadLine() returns false from the beginning. This happens on Linux!
    When I open the file with an editor, I see newlines, so I must be doing something wrong here.
    For completeness: partFile is a QTemporaryFile written by some other thread that passes the partFile pointer to this thread (the one that's is doing the reading) trough a queue system.
    I am absolutely clueless here. Is there any behaviour of QIODevice, QFile or QTemporaryFile that I'm overlooking?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QFile::atEnd() problem?

    Maybe despite the fact that you see newlines there are no newlines there?

    BTW. Accessing QObjects across threads is Very Bad, so try redesigning your architecture.

  7. #7
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::atEnd() problem?

    Quote Originally Posted by wysota View Post
    Maybe despite the fact that you see newlines there are no newlines there?
    That would be strange... But I'll look into it...

    BTW. Accessing QObjects across threads is Very Bad, so try redesigning your architecture.
    Why? I'll admit this is my first multithreaded application. I had thought that as long as I make sure the objects are only accessed by one thread at the time, it should be OK. Only one thread can write data to the objects, the other threads are only reading (i.e. object->getSomeProperty() ).

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QFile::atEnd() problem?

    Quote Originally Posted by lvi View Post
    Why?
    Because QObject is not reentrant nor thread-safe.

    I had thought that as long as I make sure the objects are only accessed by one thread at the time, it should be OK.
    The point is that you are not the only one accessing them.

    Only one thread can write data to the objects, the other threads are only reading (i.e. object->getSomeProperty() ).
    That doesn't matter. Imagine you are reading some data and at the same time some other thread changes it and as a result you read partially the new and partially the old data.

  9. #9
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::atEnd() problem?

    You make a good point. I did read http://doc.trolltech.com/4.4/threads...-thread-safety, but it seems I have not yet fully understood. If I understand correctly, I should only use the approach I use now if I properly use mutexes, or pass the object by reference in a signal-slot setup?

    To come back to the original problem: I have found that the problem may be caused my socket reading (where the data in the file comes from). Not yet sure how to fix it, but apparently, there's some \r and \n mess going on there, i.e.: \r\n from the network protocol, \n from writing to file, something like that.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QFile::atEnd() problem?

    Quote Originally Posted by lvi View Post
    If I understand correctly, I should only use the approach I use now if I properly use mutexes, or pass the object by reference in a signal-slot setup?
    Not really. You can't call directly methods from QObjects across threads unless they are explicitely mentioned as thread-safe. So yes, you can connect to them using signals and slots to transfer data across threads, but you can't pass QObjects themselves as arguments to signals or call the sending object's methods from within the slot living in another thread than the object.

    To come back to the original problem: I have found that the problem may be caused my socket reading (where the data in the file comes from). Not yet sure how to fix it, but apparently, there's some \r and \n mess going on there, i.e.: \r\n from the network protocol, \n from writing to file, something like that.
    Dump the data directly to a file without modifying anything and inspect it manually.

  11. The following user says thank you to wysota for this useful post:

    lvi (6th August 2008)

Similar Threads

  1. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  2. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.