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