how to save input in a file?

Jul 29, 2015 at 1:17pm
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 Jul 29, 2015 at 1:21pm
Jul 29, 2015 at 1:25pm
Like 4 overwrites the file every time you run this program.
Jul 29, 2015 at 1:28pm
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 Jul 29, 2015 at 1:33pm
Jul 29, 2015 at 1:58pm
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.