Results 1 to 7 of 7

Thread: Prepending to file

  1. #1
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Prepending to file

    How can I add some chars to a the beginning of an existing file without overwriting (and without copying the entire file and creating a new one!)?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Prepending to file

    you dont.


    .........
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2012
    Posts
    20
    Thanked 1 Time in 1 Post

    Default Re: Prepending to file

    You mean "can't"!

  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: Prepending to file

    Yes, he means cannot. For practical purposes this is un-possible This is nothing to do with Qt. Rather, it is a basic limitation of the high-level concept of a sequential computer file.

    With random access to the file you could try to fake it by:
    • extending the file by n bytes,
    • copying each successive block (starting at the end) along by n bytes to free n bytes at the front, then
    • put n extra bytes at the front of the file.

    You don't save on I/O and it isn't efficient (you copy the entire file and seek a lot), but you don't need two copies of the file to exist side-by-side.

    I expect a different solution to your unstated problem is a better choice.

  5. #5
    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: Prepending to file

    Quote Originally Posted by ChrisW67 View Post
    copying each successive block (starting at the end) along by n bytes to free n bytes at the front, then
    Ouch That's a beautiful case of ignoring things like caches.

    I'm guessing rewriting the whole file would be way faster. One can read an existing block of data, store it in memory (or in a separate file), write new content, read another block, write the previously saved block, etc. And of course one needs to keep his fingers crossed that no power failure takes place.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Prepending to file

    Quote Originally Posted by wysota View Post
    And of course one needs to keep his fingers crossed that no power failure takes place.
    Right
    If data integrity is important the only safe way is to write into a second file and then rename it to the orignal name. On good operating systems the rename is an atomic operation, so there is either the old file or the new file.

    Someone at KDE is working on a class for Qt5 that does that internally (Window's rename isn't atomic, the class needs some other code path there).

    Cheers,
    _

  7. #7
    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: Prepending to file

    Quote Originally Posted by anda_skoa View Post
    Right
    If data integrity is important the only safe way is to write into a second file and then rename it to the orignal name. On good operating systems the rename is an atomic operation, so there is either the old file or the new file.

    Someone at KDE is working on a class for Qt5 that does that internally (Window's rename isn't atomic, the class needs some other code path there).
    Would be nice if a file cloning facility became available too. There are low-level calls in some of filesystems that allow an atomic clone of a file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 1st November 2010, 16:33
  2. Replies: 0
    Last Post: 21st July 2010, 10:32
  3. Replies: 4
    Last Post: 9th May 2010, 16:18
  4. Replies: 3
    Last Post: 28th March 2009, 15:37
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

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.