Results 1 to 4 of 4

Thread: Question about using QFile and QTextStream

  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Question about using QFile and QTextStream

    Hi, I have a question regarding QFile and QTextStream. I know that normally we will use them like this:

    Qt Code:
    1. QFile file("filename.txt"); // create a QFile object by giving a file name(and path)
    2. if (!file.open(QIODevice::Read/WriteOnly)) {
    3. // print error
    4. return;
    5. }
    6.  
    7. QTextStream outStream(&file); // create a QTextStream object which "link" to the QFile object "file"
    To copy to clipboard, switch view to plain text mode 

    my question is: can we use QTextStream alone? That is, instead of create a QFile first and then create a QTestStream pointing to that QFile, can we create a QTextStream directly from a file name and then read or write directly on this QTextStream object?

    I used to use StreamWriter and StreamReader in C# and hope to have something similar in QT classes. So far, I have to always keep two objects, one QFile and one QTextStream, in order to read/write a file.

    Thanks for any comment!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Question about using QFile and QTextStream

    No, if you want to write or read from a file, you need to have a file descriptor.
    Either pass a FILE object to the textstream or a QIODevice object.

    You can create a subclass of course that encapsulates everything.

  3. #3
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about using QFile and QTextStream

    hi, tbscope, thank you for the quick reply !

    The task I am facing is to keep a list (QList) of txt files open during runtime and write into these files frequently, and close all files when program ends.
    Right now, I implemented in this way: each element in the QList is a struct, which consists of a QString (holding the full path of file), a QFile pointer, and a QTextStream pointer, and some other data members.
    Just wondering if there is a way to only use one pointer.
    actually, the reason I need to keep a QTextStream object is to use the ReadLine() function, since QFile doesn't have this function.
    Also I am wondering why QTextStream doesn't provide a Writeline() function...

    thanks again !

  4. #4
    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: Question about using QFile and QTextStream

    The task I am facing is to keep a list (QList) of txt files open during runtime and write into these files frequently, and close all files when program ends.
    Right now, I implemented in this way: each element in the QList is a struct, which consists of a QString (holding the full path of file), a QFile pointer, and a QTextStream pointer, and some other data members.
    Just wondering if there is a way to only use one pointer.
    You have a QList<YourStruct> or QList<YourStruct *> so you are already providing a single "pointer" access to each set of data.

    If you keep a handle on the QTextStream then you can always access the underlying QIODevice through QTextStream::device(). That will allow you to close the device (file). If you qobject_cast the QIODevice* to a QFile* then you can retrieve the QFile::fileName() also.
    Also I am wondering why QTextStream doesn't provide a Writeline() function...
    readLine() is a convenience method to read a line where it automatically handles different line ending conventions for you. The overloaded operator<<() and the related endl function provide the write functionality with the 'correct' line ending for your platform.

  5. The following user says thank you to ChrisW67 for this useful post:

    liuliuwang (14th October 2010)

Similar Threads

  1. QFile and QTextStream problems
    By uchennaanyanwu in forum Newbie
    Replies: 0
    Last Post: 7th August 2008, 02:44
  2. QFile, QTextStream
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 12th January 2008, 19:40
  3. QFile, QTextStream, seek()
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2006, 15:07
  4. Qfile and QTextStream
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2006, 10:43
  5. QSettings vs (QFile + Qtextstream)
    By nupul in forum Newbie
    Replies: 5
    Last Post: 10th April 2006, 07:26

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.