Results 1 to 5 of 5

Thread: File edition

  1. #1
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question File edition

    Hello, I have been trying to open a file in append mode and write something in a determined position of the file, I have been trying to do it like this:

    Qt Code:
    1. file.open("file.html", std::ios::out | std::ios::app);//Opens the file in output mode and append mode
    2. file.seekp(23, std::ios::end);//goes to 23 bytes(I think) before the end of file
    3. file << "something" << std::endl;//Writes that something
    To copy to clipboard, switch view to plain text mode 

    However even with the seekp function what i write always end's up at the end of the file. I would like to know how can I avoid that from happening.

    Thanks for now.
    class newbie: public QWidget { };

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: File edition

    that is the intended behaviour if you open the file for appending.

    try
    Qt Code:
    1. file.open("file.html", std::ios::out); //Opens the file in output mode
    2. file.seekp(-23, std::ios::end); //goes to 23 bytes [I]before[/I] the end of file
    3. file << "something" << std::endl;//Writes that something
    To copy to clipboard, switch view to plain text mode 

    note, however, that opening a file in ascii mode and writing out a string as done above usually does not make sense with byte counting and going to 23 bytes before the end...

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

    hazardpeter (16th August 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File edition

    Quote Originally Posted by caduel View Post
    note, however, that opening a file in ascii mode and writing out a string as done above usually does not make sense with byte counting and going to 23 bytes before the end...
    Hum, I will keep that in mind. And thanks for the answer it was really helpful for now.

    Edit: unfortunately it didn't work. I think when we open a file in append mode every think just goes to the end of the file and the seekp and seekg don't work. Can I open a file and add something there in a determined position without erasing the rest of of the file???
    Last edited by hazardpeter; 16th August 2009 at 22:52.
    class newbie: public QWidget { };

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: File edition

    when you open a file in append mode, you can seek and *read* at different positions. all written data is, however, appended (at the end):

    From the manpage of "open" (and I guess we can assume that std::ios::app will behave the same):
    APPEND Set the file pointer to the end of the file prior to each write.

    When you write data at any position (but the end), the data present at that position will be overwritten. there is no "insert 10 bytes here" mode. You would have to do that yourself.

    Finally: "didn't work"... you did notice that in my code snippet the std::ios:app was missing?

  6. #5
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File edition

    Quote Originally Posted by caduel View Post
    (...)
    Finally: "didn't work"... you did notice that in my code snippet the std::ios:app was missing?
    Lol, sorry I didn't notice that. But Thanks again for everything!
    class newbie: public QWidget { };

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  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

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.