Results 1 to 7 of 7

Thread: QFile and setTextMode

  1. #1
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QFile and setTextMode

    Hi, guys.

    I was just experimenting with QFile and reading a random file line by line and got this question. Here is what i tried

    Qt Code:
    1. #include <QFile>
    2. #include <QStringList>
    3.  
    4. #include <QtDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8.  
    9. list << "Makefile";
    10.  
    11. QList<QString>::const_iterator it;
    12.  
    13. for (it = list.begin(); it != list.end(); ++it) {
    14.  
    15. QFile file((*it));
    16. //file.setTextModeEnabled(true);
    17.  
    18. if (file.open(QFile::ReadOnly)) {
    19.  
    20. char buf[1024];
    21.  
    22. while ( 0 < file.readLine(buf, sizeof(buf)) ) {
    23. qDebug() << buf;
    24. }
    25.  
    26. file.close();
    27.  
    28. } else {
    29. qDebug() << "main> Unable to open file " << (*it);
    30. }
    31. }
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Note that setTextMode is commented.

    The i run the program and see the file printed line by line and it is ok.

    But, if uncomment setTextMode line, compile this program and run it again i always get this error:

    Qt Code:
    1. QFile::open: File (Makefile) already open
    To copy to clipboard, switch view to plain text mode 

    As far as i know the setTextMode set to true takes care of end-of-line conversion only.

    Why does this happen?

    Thanks.
    I'm a rebel in the S.D.G.

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and setTextMode

    Try to avoid setTextModeEnabled() and open the file in a text mode:
    Qt Code:
    1. if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile and setTextMode

    Thanks.
    I know that this way works, but it is very interesting to know the reason of that behavior with setTextMode.
    I'm a rebel in the S.D.G.

  4. #4
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and setTextMode

    I have run your codes on my computer, and I face the same problem.
    Learning……
    Good luck!

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and setTextMode

    I suppose you must use QIODevice::setTextModeEnabled on a opened File
    A camel can go 14 days without drink,
    I can't!!!

  6. #6
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and setTextMode

    if we use QIODevice::setTextModeEnabled on an opened file, the status of the file cannot be shown on the screen. There is no difference between set this true and false.

  7. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and setTextMode

    From Qt Documentation
    void QIODevice::setTextModeEnabled ( bool enabled )

    If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. This feature is useful for classes that provide custom end-of-line handling on a QIODevice.
    seems that you can use the function to customize end of line handling for QIODevice subclass
    A camel can go 14 days without drink,
    I can't!!!

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.