Saturday, May 22, 2010

How do I send information to a file from a main program and a called function in c++?

keywords: ofstream,


the catch: dont want to delete information already sent to file in a program. I just want to send more information to the file, without losing what i already have.

How do I send information to a file from a main program and a called function in c++?
Look at the i/o flags for ofstreams. They are the second parameter to either the ofstream constructor or its open() method.





It's not entirely clear to me what you want to do but it looks as if you want ios::ate or ios::app. The former stands for "at end" and opens the file and puts the write pointer at the end so you are appending data and not overwriting existing info. However with ios::ate you can move the write ptr manually and overwrite info if you so choose. ios::app is essentially the same as ios::ate but it *always* writes (appends) data to the end of the file regardless of where you might have moved the write pointer.





So you want something like:





ofstream myfile("somestuff.dat", ios::app);


No comments:

Post a Comment