Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:39:09+00:00 2026-06-18T06:39:09+00:00

std::ofstream ofs; ofs << "Hello, world!" << endl; Now I want to modify the

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:39:10+00:00Added an answer on June 18, 2026 at 6:39 am

    The std::endl IO 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::ostringstream to 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: std::ofstream f(file1.txt); f<<123<<std::endl<<456; //(*1) /*std::stringstream ordinary_strstream; This works too ordinary_strstream<<123<<'\n'<<456;
I am writing strings to a text file using: ofstream ofs(ofs.txt, ios_base::binary); std::string str
i have a function defined as follows: void AddHeadCode(std::ofstream &ostream, size_t length){ ostream.write((char*)length, sizeof(length));
I'm trying to do this: #pragma once #include <fstream> #include <string> static std::ofstream ErrorLog;
I'm converting a project from vs2008 to vs2010 and getting linker errors for std:ifstream/ofstream
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);
I have some code that writes the system time to a file: std::ofstream file(time.txt);
In this MSDN article on file sharing mode with std::ofstream , Microsoft writes: To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.