Results 1 to 3 of 3

Thread: QTextStream and file handle

  1. #1
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextStream and file handle

    Hi,
    I have a query regarding Files and textstream in QT

    if I have a code such as the following :

    Qt Code:
    1. QFile file("abc.txt");
    2. file.open(QIODevice::ReadOnly);
    3. QTextStream(&file);
    4.  
    5. file.setFileName("xyz.txt");
    6. file.open(QIODevice::ReadOnly);
    To copy to clipboard, switch view to plain text mode 


    In the above case what should QTextstream be pointing at?
    Contents of "abc.txt" or "xyz.txt"
    In my case it points to "abc.txt"
    Is there any way I can change it to point to "xyz.txt"?

    Thank you in advance.
    Last edited by high_flyer; 4th January 2011 at 11:16. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream and file handle

    In my case it points to "abc.txt"
    This surprises me, and I tend to think that either the code you posted is not 100% the same as you really use it, or, something else that you do.
    The stream is not 'pointing' to anything, it takes a reference to a QFile (actually a QIODevice) object.
    This means, the stream doesn't know anything about the file name - it just streams to the device it has a reference to, and that device may have any filename what so ever - the QFile object is reposible for opening the physical file, not the stream.

    Make sure you are not setting a different file name to an already open QFile object.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    Dilshad (4th January 2011)

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

    Default Re: QTextStream and file handle

    In this case I would advice you to go with the core python paradigm: Explicit is better than implicit. Remember that you will be having to read and maintain this code at some point, and take it from me, you'll have forgotten how this piece of code was supposed to work in three weeks time. Create a new file and a new text stream to avoid any confusion now and in the future.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Replies: 0
    Last Post: 17th June 2010, 11:07
  2. writing file with QTextStream
    By Raul in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2010, 14:46
  3. Replies: 5
    Last Post: 27th May 2009, 12:49
  4. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47
  5. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12

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.