[cs23021] appending to a file
Mikhail Nesterenko
mikhail at cs.kent.edu
Wed Feb 22 13:04:31 EST 2012
CSI students,
As we discussed today. Ordinarily, if an existing file is opened for
writing, all its contents is overwritten. However, there is a way to
keep the existing contents and just append the new input to the
file. For that, open() function may take an additional argument called
mode. Mode "ios:app", means that the file is opened for writing with
appending. For example, the following code writes 0 1 2 3 4 to the
file.
#include <fstream>
using namespace std;
int main() {
ofstream out_stream;
for(int i=0; i < 5; i++){
out_stream.open("outfile.txt", ios::app);
out_stream << i << " ";
out_stream.close();
}
}
Thanks,
--
Mikhail
More information about the cs23021-2
mailing list