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 9283639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:40:55+00:00 2026-06-18T18:40:55+00:00

For example, I want to write std::string my_str( foobar\n ); literally into std::ostream& with

  • 0

For example, I want to write std::string my_str( "foobar\n" ); literally into std::ostream& with the backslash-n intact (no formatting).

Perhaps I need to convert my_str with a formatting function that converts backslash to double-backslash first? Is there a standard library function for that?

Or maybe there is a directive I can pass to std::ostream&?

  • 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-18T18:40:57+00:00Added an answer on June 18, 2026 at 6:40 pm

    The easiest way to do this for larger strings is with raw string literals:

    std::string my_str(R"(foobar\n)");
    

    If you want parentheses in it, use a delimiter:

    R"delim(foobar\n)delim"
    

    I don’t know of anything that will let you keep the escape codes etc. in the string, but output it without, but std::transform with an std::ostream_iterator<std::string> destination and a function that handles the cases you want should do it, as it does in this example:

    std::string filter(char c) {
       if (c == '\n') return "\\n";
       if (c == '\t') return "\\t";
       //etc
       return {1, c};
    }
    
    int main() {
       std::string str{"abc\nr\tt"};
       std::cout << "Without transform: " << str << '\n';
    
       std::cout << "With transform: ";
       std::transform(std::begin(str), std::end(str), std::ostream_iterator<std::string>(std::cout), filter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that slices a 'string' into a vector, sequentially,
For example, I want to write an activity that launches when you return from
I want to create URL re-write rules for following URL in htaccess. http://example.com/vedios/category/fun/ ->
I want to write a function that outputs something to a ostream that's passed
For example I want to write this JS code $(document).ready(function() { var parent =
I can make an std::ostream object output integer numbers in hex, for example std::cout
I want to read and write NaN values from/into text files using iostream and
For example, I want to write overload functions for set_difference which compares the type
Can I specify what file I want to write into in C++? I want
I want to checkout svn using gradle. According to example I wrote this task

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.