how to save input in a file?

I am writing this little test program to see if I can save input into a file without it being overwritten, however i cannot.

here is the following code:

1
2
3
4
5
6
7
8
9
10
  int main()

	std::ofstream someFile("file.dat");
	someFile << "" << std::endl;
	someFile.close();

	std::fstream somefFile("file.dat",ios::in | ios::out | ios::app);
        std::string input;
	std::getline(cin, input);
        somefFile << input << std::endl;


However, whenever I enter new input by restarting the program, the old one is overwritten.


does anyone know how to save the old input?

please and thank you.


Last edited on
Like 4 overwrites the file every time you run this program.
yup

for example if i input "hi", then run this program again and input "hello"," hello" will overwrite "hi". however it append perfectly fine for strings that have not been inputted by the user
Last edited on
So, if you want to save the previous input, either open your ofstream for append (ios::app) or don't write to it on line 4.
Topic archived. No new replies allowed.