0

i know that its so easy. but i dont know how to do it. ive tried ifstream and ofstream to add a line in a text file. and im sure someone in the past may have already posted this kind of problem. can anyone give me a link to that thread? thanks.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jogieglenmait
0
std::stringstream ss;
ss << "A line of text."
std::ofstream file("lines.txt", std::ios::out | std::ios::app);
if(file.is_open()) file << ss.str() << '\n';
file.close();

Edit: Couple notes
You may want to read about ofstream flags, they are non-trivial.
Closing the file is important.
You don't need to use a stringstream like I did.
You should definitely check to make sure you opened the file. (shown above)

Edited by Unimportant: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.