Thread: writing to the beginning of a file

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    writing to the beginning of a file

    Hi all,

    I have a file on c:\ and wish to write to the beginning of it, but not overwrite any of the content that is already there. Basically the contents of the file looks like this

    This is line 2
    This is line 3
    This is line 4
    This is line 5

    I wish to open this file and write "This is line 1" at the beginning of it so it looks (suprisingly) like this

    This is line 1
    This is line 2
    This is line 3
    This is line 4
    This is line 5

    This is my code so far

    Code:
    	ofstream out("c:/myfile.txt", ios::app);
    	out.seekp(0, ios::beg);
    	out<<"This is line 1\r\n";
            out.close();
    But this still writes to the end of the file.

    Any ideas?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    what you could do is open a new file for writing, write line 1 then copy the data from the other file in. Delete old file (using system function) and rename the new file. I know there is a function to del and rename but I don't know their names.
    Amish

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    thanks for the reply :-)

    Surely there is a more elegant way to do this than to create different files.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Nope.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can read the original contents into an STL container of some sort. You can then insert the new line at the beginning of the container. Close and reopen the original file for writing and write out the contents of the container back to the original file. This means you don't need to create a new file, delete of the old file, and rename the new file back to the old name. Still, it's not a "pretty" process, there is no clean file insertion function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Nope.
    Yep. Read the contents of the file into a string. Write the new line to the file, then write the contents of the string back to the file.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    or write the line you want inserted first to a string , concatenate that file to the string, then output it to the file.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Close and reopen the original file for writing and write out the contents of the container back to the original file.
    It does on the other hand mean that you've either got the old file or the new file.
    Bugs / crashes / power fails will ensure that you end up with nothing if you simply re-use the existing file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 12:34 PM