Results 1 to 5 of 5

Thread: How to read changing file contents

  1. #1
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to read changing file contents

    I've a file named 'file.txt'. Its contents changes with time. How can i read it? I tested it using QFile class. But once read, data is not changing until application restarts.
    Then i tried making file object and destructing each time i read the file, but no use. Once the file is read there's no change in the contents. Is there any way?

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to read changing file contents

    I can see few options here:
    1. calculate MD5/CRC (any hash) of the file that You read, store that hash and periodically compute new one and compare it with the one that You store, is oldHash != newHash file.readAll() etc.
    2. check file attribute: last modified and store it after Your last access (from Your program) and compare it periodically

    probably there are more.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read changing file contents

    Try using QFileSystemWatcher and connect to the fileChanged() signal. When the signal is sent with the name of the file you are interested in, then you know it has changed. At that point, you can re-read the file.

    When you read the file with QFile, you basically create a copy in memory of the contents of the file at the time you read it. This in-memory data does not change when the file changes, because it is a copy.

    If you want the in-memory data to be up to date, then you need to watch the file for changes (which QFileSystemWatcher will do for you) and re-read the file when you see it has changed. Be sure to close the file after you read it, otherwise the other app that is changing it may not be able to open it if you have it open and locked.
    Last edited by d_stranz; 9th July 2011 at 22:43.

  4. #4
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read changing file contents

    The method both of you described to find the change in file may be correct. But actually that isn't my problem.
    call readAll()
    a change is made in the file externally.
    call readAll()
    in both calls, i am getting same result. That means, the change is not seen on the output.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read changing file contents

    Close the file, then reopen it, or create a new QTextStream each time you want to read it. QTextStream caches the file contents on the first readAll(), so when you ask for it again, you get the same thing. You could try calling seek( 0 ), but that still might not empty the cache.

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Replies: 8
    Last Post: 17th June 2011, 00:35
  3. Display contents of .txt file
    By karmo in forum Newbie
    Replies: 17
    Last Post: 23rd January 2010, 20:39
  4. QTableView and changing how a cell displays its contents
    By BitRogue in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 10:18

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.