Std::String Formatting Like Sprintf

std::string formatting like sprintf

You can't do it directly, because you don't have write access to the underlying buffer (until C++11; see Dietrich Epp's comment). You'll have to do it first in a c-string, then copy it into a std::string:

  char buff[100];
snprintf(buff, sizeof(buff), "%s", "Hello");
std::string buffAsStdStr = buff;

But I'm not sure why you wouldn't just use a string stream? I'm assuming you have specific reasons to not just do this:

  std::ostringstream stringStream;
stringStream << "Hello";
std::string copyOfStr = stringStream.str();

Using sprintf with std::string in C++

Your construct -- writing into the buffer received from c_str() -- is undefined behaviour, even if you checked the string's capacity beforehand. (The return value is a pointer to const char, and the function itself marked const, for a reason.)

Don't mix C and C++, especially not for writing into internal object representation. (That is breaking very basic OOP.) Use C++, for type safety and not running into conversion specifier / parameter mismatches, if for nothing else.

std::ostringstream s;
s << "Type=" << INDEX_RECORD_TYPE_SERIALIZATION_HEADER
<< " Version=" << FORMAT_VERSION
// ...and so on...
;
std::string output = s.str();

Alternative:

std::string output = "Type=" + std::to_string( INDEX_RECORD_TYPE_SERIALIZATION_HEADER )
+ " Version=" + std::to_string( FORMAT_VERSION )
// ...and so on...
;

std::string formatting like sprintf

You can't do it directly, because you don't have write access to the underlying buffer (until C++11; see Dietrich Epp's comment). You'll have to do it first in a c-string, then copy it into a std::string:

  char buff[100];
snprintf(buff, sizeof(buff), "%s", "Hello");
std::string buffAsStdStr = buff;

But I'm not sure why you wouldn't just use a string stream? I'm assuming you have specific reasons to not just do this:

  std::ostringstream stringStream;
stringStream << "Hello";
std::string copyOfStr = stringStream.str();

String.Format alternative in C++ [duplicate]

You can use sprintf in combination with std::string.c_str().

c_str() returns a const char* and works with sprintf:

string a = "test";
string b = "text.txt";
string c = "text1.txt";
char* x = new char[a.length() + b.length() + c.length() + 32];

sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() );

string str = x;
delete[] x;

or you can use a pre-allocated char array if you know the size:

string a = "test";
string b = "text.txt";
string c = "text1.txt";
char x[256];

sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() );

C++ string formatting like Python {}.format

In C++20 you'll be able to use std::format which brings Python-like formatting to C++:

auto s = std::format("{:10}", "some_string");

Until then you can use the open-source {fmt} formatting library, std::format is based on.

Disclaimer: I'm the author of {fmt} and C++20 std::format.

sprintf with %s and std::string gives gibberish

The '%s' modifier of printf takes a char*, not a std::string.

You can write:

sprintf(command,"echo \"something with a string %s\" ", stringz.c_str());

Which gives you a const char* to the contents of a std::string. This shows one of the major weaknesses of sprintf -- no type checking!

Generating compile time functions string for formatting strings with libfmt

The type of the format string and the return type of the function cannot be string_view since the format string is constructed dynamically, using string_view will result in a dangling pointer.

In addition, fmt::format requires that the format string must be a constant expression. Instead, you need to use fmt::vformat. This should work

static std::string
headerCenter(const std::string& text, const int width, const char fill) {
// build fmt string
std::string format = fmt::format("|{{0:{}^{}}}|", fill, width);
return fmt::vformat(format, fmt::make_format_args(text));
}

Demo



Related Topics



Leave a reply



Submit