CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    41

    Opening a file without overwriting it!

    Hi

    I have been searching for a solution for this for ages but could not find anything:

    I have a binary file which I want to open and modify a value. However when I use something like:

    ofstream outfile(filename,ios:ut|ios::binary);

    the code overwrites the file.

    What should I be using so I don't overwrite the existing file?

    I am using Visual C++ v6.

    Cheers

    Robbie

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Opening a file without overwriting it!

    Add ios::in .. that will prevent file truncation (but the file must exist).
    Then seekp() to the spot you want to overwrite.

    Code:
    ofstream outfile(filename,ios::in|ios::out|ios::binary);

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Opening a file without overwriting it!

    You could use the ios::app mode if you want to append data to the end of an existing file.

    - petter

  4. #4
    Join Date
    Apr 2005
    Posts
    41

    Re: Opening a file without overwriting it!

    Cheers Philip, I will try that tonight.

  5. #5
    Join Date
    Jun 2007
    Posts
    54

    Re: Opening a file without overwriting it!

    use

    ios::app...

    That shud solve your problem..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured