all I have a one small program in c++.I need to store some text in the .txt file it stored and file also created but again i run with some other data the previous data is deleted in .txt file, help anyone how to solve that problem i ask doubt is it possible in c++ yes/no.someone help!
Asked
Active
Viewed 729 times
-2
-
You need to check if the file exists first and either append or not overwrite – EdChum Dec 19 '18 at 10:45
-
1Welcome to SO. Please provide a Minimal, Complete, and Verifiable example. **Show us the code for your latest attempt** and where you got stuck. and explain why the result is not what you expected. Edit your question to include the code, please don't add it in a comment, as it will probably be unreadable. https://stackoverflow.com/help/mcve It is better to show what is actually happening, rather than describing what you expect to happen. Please include code and output as content for your question, not as pictures or external links. – Dragonthoughts Dec 19 '18 at 10:45
1 Answers
1
The behavior you are describing is called overwriting a file.
You have two choices, if the file already exists:
- Append to already existing file. Read more in How to append text to a text file in C++?
- Write the data to a different file.

gsamaras
- 71,951
- 46
- 188
- 305