Thread: Writing to a file without overwriting

  1. #1
    Unregistered
    Guest

    Writing to a file without overwriting

    I need to know how to write to a file without overwriting stuff in it.

    This is what my program needs to do.
    its opens up a c++ file
    looks for the main function
    and inserts a few lines of code into the main function
    i can get it to write inside the main function
    but when i write, it overwrites whats already there
    like when u have insert pressed down in Word

    Any help would be great.

    Thanks

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you need to set the output in "append" mode....

    for example:

    Code:
    ofstream fout("c:/My Documents/TextFile.txt", ios::app);
    in this example whatever you want to add to a file will be inserted after the last item in a file thus no items will be overwritten.....

    Good Luck

    matheo917

  3. #3
    Unregistered
    Guest
    I need to write to the middle of the file though, so i'm using ios::ate. I'm also using tellg and seekp to place the cursor at the point where i want to write.

    cursor = inFile.tellg();

    cout << cursor;

    outFile.open( "test.cpp", ios::ate);

    if(!outFile){
    cerr << "File could not be opened." << endl;
    exit (1);
    }

    outFile.seekp( cursor );

    char coutstring[ 80 ] = "string";

    outFile.write( "cout << virus << endl;\n", 20);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  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