Printing to file, multiple program runs.

Hey, I'm working on a voting machine, and I ran into a problem.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
    ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}


What i need is for when you run the program a second time, instead of it just overriding what is already on the file, it'll just add on to it.

Thanks for the help.
Then use the ios::app flag as a second parameter for open(); function.

eg: myfile.open ("example.txt",ios::app); //meaning append

Hope it helps,
Aceix.
Topic archived. No new replies allowed.