Re: c++ fstream writing to existing file

From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 07/24/04


Date: 24 Jul 2004 10:39:42 -0700

ycmahesh@hotmail.com (Mahesha) wrote in message news:<3d09d707.0407231701.3d611ce5@posting.google.com>...
> Hello,
> I'm new to C++ and I have requirement to open a existing text file in
> write mode and write 2 new lines of text in the beginning of the file.
> I'm working with fstream standard library.
> If someone could direct me to a sample code to do this that would be
> really helpful.

Most file systems will only let you add to the end of a file. The
usual way to handle this is:
1. copy current file contents to a temporary file.
2. rewind the original file.
3. write your two lines to the file.
4. copy the contents from the temporary file back to the original.

-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.


Relevant Pages

  • RE: Locked for editing???
    ... removable disk, such as a floppy diskette, the disk has been removed. ... file name and places this file in the same folder as the original file. ... The process was interrupted while creating the temporary file because you ... If the interruption was caused ...
    (microsoft.public.excel.setup)
  • Re: Newbie howto question.
    ... I can identify the tagged information, ... >said info and write it to the html file, ... create a new temporary file for writing ... read the first line from original file. ...
    (comp.programming)
  • Re: Serialized r/w access to a collection from multiple processes - DLL or EXE?
    ... one or more instances of our app EXEs will be accessing the same ... Flush memory content into new temporary file. ... If write succeeds, then rename original file. ...
    (microsoft.public.vc.language)
  • RE: How is the ~$... file created when you open a document used?
    ... This temporary file holds the logon name of the person ... If the Owner File is damaged or missing the error message changes to: ... Word automatically deletes the Owner File when the original file is closed. ... I understand that Word opens a "copy" of the file and you ...
    (microsoft.public.word.docmanagement)