std::ofstream ofs;
ofs << "Hello, world!" << endl;
Now I want to modify the contents of ofs to "Hello, money!" before writting to disk.
How can I implement it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
std::endlIO manipulator will flush the content of the stream buffer, so you will have to change it into a'\n'if you later want to process the stream before its content gets flushed.Also, under the assumption that the reason you actually want to do this is because you need to manipulate the string which is going to be written after formatting, I shall make you aware of the fact that you can use an
std::ostringstreamto exploit the functionality of formatted streaming and gather the result into a string, which you can then manipulate and normally write to a file.If this was obvious information for you and your use case is more complex, then you will have to write your own stream buffer, as pointed out by @MatsPetersson.