Thread: Inserting into file without overwriting?

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    Question Inserting into file without overwriting?

    How do I insert data at the beggening of the file so that it moves everything over and doesn't overwrite the existing data?
    Do I have to write to another tempory file the data I want to add then read in existing file and append it to the temporary file in order to get that result? Is there a better way?



    file1.txt:
    Hendrix

    file1.txt after adding data the way I want it:
    Jimi Hendrix

    instead of like this:
    Jimirix
    Last edited by WebSnozz; 04-21-2002 at 04:40 AM.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You have to use the append mode

    ios::app


    but before that you will have to follow your method if you want it to append to the begining of file..

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I think you are going to have to read the "hendrix" part into a string, then clear the file, and put "jimi" and your string back in.

    I have been having a similiar problem with a numerical file, and our genious in residence has been less then forthcoming on how to use direct offsets in a text file.

  4. #4
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I was afraid of that. I think I'll read the letters one at a time, push the char onto a STL queu, write my new char, and do that in a loop until I'm done writing and then continue in another loop that pushes the next char onto the queu and pops the last one of and writes it that way I don't have to make another file.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Haven't investigated this one myself (sorry) but have you tried writing to ios::cur?

    Upon opening, as everyone knows, the pointer is set at the beginning of the file.

    THe default, of course, is 'ios::trunc', but I don't know if, when you write at 'ios::cur' (current), you can "push" your existing file.

    Just a thought.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM