Results 1 to 3 of 3

Thread: how can i clear a file?

  1. #1
    Join Date
    Sep 2008
    Location
    Slovakia, Nitra
    Posts
    15
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how can i clear a file?

    Hi,
    How can I clear content of a file?!
    For example a write to file.dat string ("how are you doing?"), after that i seek it in to begining, then i write some shorter string ("hello"), after that i get in file.dat something like: "hellore you doing?"
    I checked the documentation of qt, but i don't understand very well to english, also tried a many version of potential code, but nothing worked for me

    Further i'm interested of delete of some interval/distance of content of a file, for example if I want delete
    • from the (quint64) 10 to (quint64) 220
    • from (quint64) 10 to end of line?


    Sorry for that stupid questions, but i played with it a long time without any succes. - also sorry for my english!

  2. #2
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how can i clear a file?

    Hi,
    I guess u use something like this:
    QFile file("file.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);

    if you want to clear content of file.dat u should creat QDataStream object with open mode like this
    QDataStream in(&file, QIODevice::Truncate);

    This should help

    Radek

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

    Vincenzo (18th January 2009)

  4. #3
    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: how can i clear a file?

    Let's make something clear first - you can't "remove" some part in the middle of a file, you can only overwrite or truncate it. The former you do with just calling write() with new data and the latter with using QFile::resize(). So in your situation, you can either reopen the file in truncate mode:
    Qt Code:
    1. QFile file("...");
    2. file.open(QFile::WriteOnly|QFile::Truncate);
    To copy to clipboard, switch view to plain text mode 

    or resize the file after writing the new contents:
    Qt Code:
    1. file.write("new contents");
    2. file.resize(file.pos());
    To copy to clipboard, switch view to plain text mode 

    Note that this is nothing specific to Qt - files are meant to work like that.

  5. The following 4 users say thank you to wysota for this useful post:

    alizadeh91 (2nd October 2012), chandan (12th August 2010), michael. (27th April 2012), Vincenzo (18th January 2009)

Similar Threads

  1. Apparent error in QtCore/quuid.h
    By cwp500 in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2008, 20:51
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

Tags for this Thread

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.