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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:22:32+00:00 2026-06-05T06:22:32+00:00

I am programming in C++. As basic as this question is I cannot seem

  • 0

I am programming in C++.

As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem:

I want to create a C-style string however I want to put an integer variable i into the string. So naturally, I used a stream:

stringstream foo;
foo
                << "blah blah blah blah... i = " 
                << i
                << " blah blah... ";

However, I need to somehow get a C-style string to pass to a function (turns out foo.str() returns an std::string). So this is technically a three part question —

1) How do I convert std::string to a C-style string?

2) Is there a way to get a C-style string from a stringstream?

3) Is there a way to use a C-style string directly (without using stringstreams) to construct a string with an integer variable in 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-05T06:22:33+00:00Added an answer on June 5, 2026 at 6:22 am

    1) How do I convert std::string to a C-style string?

    Simply call string.c_str() to get a char const*. If you need a mutable _C-style_ string then make a copy. The returned C string will be valid as long as you don’t call any non-const function of string.

    2) Is there a way to get a C-style string from a stringstream?

    There is, simply strstream.str().c_str(). The returned C string will be valid only until the end of the expression that contains it, that means that is valid to use it as a function argument but not to be stored in a variable for later access.

    3) Is there a way to use a C-style string directly (without using stringstreams) to construct a string with an integer variable in it?

    There is the C way, using sprintf and the like.

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

Sidebar

Related Questions

This is very basic question from programming point of view but as I am
I am new to thread programming in Java and hence this basic question. (I
this question may seem too basic to some, but please bear with be, it's
I have a basic question about MPI programming in C. Essentially what I want
I know this is a very basic question. I am new to web programming.Im
I'm learning the basics of programming here (C#) but I think this question is
This is more of a business-oriented programming question that I can't seem to figure
Pretty basic programming question, I know PHP have a function for it, but does
I've got another question regarding to basic Android programming: How can I access the
Probably a basic question, but new to Rails and web programming and haven't found

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.