std::string mystring;
sprintf(mystring.c_str(), "%s %d", "Helloworld", 2014);
Its is giving a compiler error to me:
'sprintf' : cannot convert parameter 1 from 'const char *' to 'char *'
std::string mystring;
sprintf(mystring.c_str(), "%s %d", "Helloworld", 2014);
Its is giving a compiler error to me:
'sprintf' : cannot convert parameter 1 from 'const char *' to 'char *'
It shouldn't be a warning, it should be an error. The pointer
returned by std::string::c_str()
points to read-only memory;
any attempt to write through it is undefined behavior. (In your
case, if you use a const_cast
to shut up the compiler, you're
code will probably crash, since you're calling c_str()
on an
empty strying.)
Generally speaking, what you probably want is
std::ostringstream
:
std::ostringstream formatter;
formatter << "Helloworld" << ' ' << 2014;
std::string myString = formatter.str();
FWIW: sprintf
is one of the most dangerous functions in the
standard library, and only present for historical reasons. It's
almost impossible to use safely; even in C, you should prefer
snprintf
(but in C++, std::ostringstream
is far better).
your warning gives you all information you need.
std::string::c_str()
returns a const char*
and sprintf requires a char*
since it modiefies the buffer.
But you are writing c++ and should avoid sprintf. Use a ostringstream
to write formated data to a string.
std::string
manages underlying C-style buffer. c_str
returns const char* because it shouldn't be modified by anything other then string's methods.
You should rather use ostringstream
. See this question: C++ equivalent of sprintf?
You are telling sprintf to store the result in mystring.c_str()
. This is a readonly view of the underlying representation of mystring
. Since it is readonly (or const char *
), you can't write the result to it.
If you need to use sprintf, you will have to create a writable character buffer for it to use, and then assign that buffer to mystring.
A different way of performing this sort of operation without having to create character buffers and deal with possible overflow would be to use a stringstream
:
std::stringstream buffer;
buffer << "Helloworld " << 2014;
mystring = buffer.str();
Alternatively, use old C snprintf into a temporary buffer, then assign into the string :
std::string mystring;
char buf[64];
snprintf(buf, sizeof(buf), "%s %d", "Helloworld", 2014);
mystring.assign(buf);
snprintf
is always safer than sprintf
since it avoids buffer overflow.
But of course using an ostringstream
like answered here by James Kanze is better.
#include <iostream>
#include <string>
#include <sstream>
std::stringstream ss;
ss << "Helloworld";
ss << " ";
ss << "2014";
ss << std::endl;
std::string str = ss.str();
std::cout << str;
const char * mystring= str.c_str();
In this way also you can append int or long number into the string.
Example:
long year = 2014;
std::stringstream ss;
ss << "Helloworld";
ss << " ";
ss << year;
ss << std::endl;
std::string str = ss.str();
std::cout << str;
const char * mystring= str.c_str();