Writing to files

Hello guys, I need help, I want to write the results of the cin to the fie but it always rewrites the file, how do I just make it open the file if it's already there if not it will ask the user to make that file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

    ofstream myfile;
string Product;
Product = "NONE";
cout << "Hello, we are Say-Pear, what would you like to see in our store? \n";
cout << "Please use the underscore _ for spaces: \n";
cin >> Product;
myfile.open ("Products.txt");
  myfile << Product;
  myfile.close();


}
Use the file mode ios::app if you want to add data at the end of the file without deleting the previous contents.
ty
Topic archived. No new replies allowed.