std::string formatting like sprintf

2018-12-31 14:44发布

问题:

I have to format std::string with sprintf and send it into file stream. How can I do this?

回答1:

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();


回答2:

C++11 solution that uses vsnprintf() internally:

#include <stdarg.h>  // For va_start, etc.

std::string string_format(const std::string fmt, ...) {
    int size = ((int)fmt.size()) * 2 + 50;   // Use a rubric appropriate for your code
    std::string str;
    va_list ap;
    while (1) {     // Maximum two passes on a POSIX system...
        str.resize(size);
        va_start(ap, fmt);
        int n = vsnprintf((char *)str.data(), size, fmt.c_str(), ap);
        va_end(ap);
        if (n > -1 && n < size) {  // Everything worked
            str.resize(n);
            return str;
        }
        if (n > -1)  // Needed size returned
            size = n + 1;   // For null char
        else
            size *= 2;      // Guess at a larger size (OS specific)
    }
    return str;
}

A safer and more efficient (I tested it, and it is faster) approach:

#include <stdarg.h>  // For va_start, etc.
#include <memory>    // For std::unique_ptr

std::string string_format(const std::string fmt_str, ...) {
    int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */
    std::unique_ptr<char[]> formatted;
    va_list ap;
    while(1) {
        formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */
        strcpy(&formatted[0], fmt_str.c_str());
        va_start(ap, fmt_str);
        final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
        va_end(ap);
        if (final_n < 0 || final_n >= n)
            n += abs(final_n - n + 1);
        else
            break;
    }
    return std::string(formatted.get());
}

The fmt_str is passed by value to conform with the requirements of va_start.

NOTE: The \"safer\" and \"faster\" version doesn\'t work on some systems. Hence both are still listed. Also, \"faster\" depends entirely on the preallocation step being correct, otherwise the strcpy renders it slower.



回答3:

Utilising C++11 std::snprintf, this becomes a pretty easy and safe task. I see a lot of answers on this question that were apparently written before the time of C++11 which use fixed buffer lengths and vargs, something I would not recommend for safety, efficiency and clarity reasons.

#include <memory>
#include <iostream>
#include <string>
#include <cstdio>

using namespace std; //Don\'t if you\'re in a header-file

template<typename ... Args>
string string_format( const std::string& format, Args ... args )
{
    size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for \'\\0\'
    unique_ptr<char[]> buf( new char[ size ] ); 
    snprintf( buf.get(), size, format.c_str(), args ... );
    return string( buf.get(), buf.get() + size - 1 ); // We don\'t want the \'\\0\' inside
}

The code snippet above is licensed under CC0 1.0.

Line by line explanation:

Aim: Write to a char* by using std::snprintf and then convert that to a std::string.

First, we determine the desired length of the char array.

From cppreference.com:

Return value

[...] If the resulting string gets truncated due to buf_size limit, function returns the total number of characters (not including the terminating null-byte) which would have been written, if the limit was not imposed.

This means that the desired size is the number of characters plus one, so that the null-terminator will sit after all other characters and that it can be cut off by the string constructor again. This issue was explained by @alexk7 in the comments.

Then, we allocate a new character array and assign it to a std::unique_ptr. This is generally advised, as you won\'t have to manually delete it again.

Note that this is not a safe way to allocate a unique_ptr with user-defined types as you can not deallocate the memory if the constructor throws an exception!

After that, we can of course just use snprintf for its intended use and write the formatted string to the char[] and afterwards create and return a new std::string from that.


You can see an example in action here.


If you also want to use std::string in the argument list, take a look at this gist.


Additional information for Visual Studio users:

As explained in this answer, Microsoft renamed std::snprintf to _snprintf (yes, without std::). MS further set it as deprecated and advises to use _snprintf_s instead, however _snprintf_s won\'t accept the buffer to be zero or smaller than the formatted output and will not calculate the outputs length if that occurs. So in order to get rid of the deprecation warnings during compilation, you can insert the following line at the top of the file which contains the use of _snprintf:

#pragma warning(disable : 4996)


回答4:

boost::format() provides the functionality you want:

As from the Boost format libraries synopsis:

A format object is constructed from a format-string, and is then given arguments through repeated calls to operator%. Each of those arguments are then converted to strings, who are in turn combined into one string, according to the format-string.

#include <boost/format.hpp>

cout << boost::format(\"writing %1%,  x=%2% : %3%-th try\") % \"toto\" % 40.23 % 50; 
// prints \"writing toto,  x=40.230 : 50-th try\"


回答5:

Unfortunately, most of the answers here use varargs which are inherently unsafe unless you use something like GCC\'s format attribute which only works with literal format strings. You can see why these functions are unsafe on the following example:

std::string format_str = \"%s\";
string_format(format_str, format_str[0]);

where string_format is an implementation from the Erik Aronesty\'s answer. This code compiles, but it will most likely crash when you try to run it:

$ g++ -Wall -Wextra -pedantic test.cc 
$ ./a.out 
Segmentation fault: 11

It is possible to implement a safe printf and extend it to format std::string using (variadic) templates. This has been done in the {fmt} library, which provides a safe alternative to sprintf returning std::string:

std::string format_str = \"The answer is %d\";
std::string result = fmt::sprintf(format_str, 42);

{fmt} keeps track of the argument types and if the type doesn\'t match format specification there is no segmentation fault, just an exception or a compile-time error if constexpr format string checks are used.

Disclaimer: I\'m the author of {fmt}.



回答6:

If you only want a printf-like syntax (without calling printf yourself), have a look at Boost Format.



回答7:

I wrote my own using vsnprintf so it returns string instead of having to create my own buffer.

#include <string>
#include <cstdarg>

//missing string printf
//this is safe and convenient but not exactly efficient
inline std::string format(const char* fmt, ...){
    int size = 512;
    char* buffer = 0;
    buffer = new char[size];
    va_list vl;
    va_start(vl, fmt);
    int nsize = vsnprintf(buffer, size, fmt, vl);
    if(size<=nsize){ //fail delete buffer and try again
        delete[] buffer;
        buffer = 0;
        buffer = new char[nsize+1]; //+1 for /0
        nsize = vsnprintf(buffer, size, fmt, vl);
    }
    std::string ret(buffer);
    va_end(vl);
    delete[] buffer;
    return ret;
}

So you can use it like

std::string mystr = format(\"%s %d %10.5f\", \"omg\", 1, 10.5);


回答8:

[edit \'17/8/31] Adding a variadic templated version \'vtspf(..)\':

template<typename T> const std::string type_to_string(const T &v)
{
    std::ostringstream ss;
    ss << v;
    return ss.str();
};

template<typename T> const T string_to_type(const std::string &str)
{
    std::istringstream ss(str);
    T ret;
    ss >> ret;
    return ret;
};

template<typename...P> void vtspf_priv(std::string &s) {}

template<typename H, typename...P> void vtspf_priv(std::string &s, H h, P...p)
{
    s+=type_to_string(h);
    vtspf_priv(s, p...);
}

template<typename...P> std::string temp_vtspf(P...p)
{
    std::string s(\"\");
    vtspf_priv(s, p...);
    return s;
}

which is effectively a comma-delimited version (instead) of the sometimes hindering <<-operators, used like this:

char chSpace=\' \';
double pi=3.1415;
std::string sWorld=\"World\", str_var;
str_var = vtspf(\"Hello\", \',\', chSpace, sWorld, \", pi=\", pi);


[edit] Adapted to make use of the technique in Erik Aronesty\'s answer (above):

#include <string>
#include <cstdarg>
#include <cstdio>

//=============================================================================
void spf(std::string &s, const std::string fmt, ...)
{
    int n, size=100;
    bool b=false;
    va_list marker;

    while (!b)
    {
        s.resize(size);
        va_start(marker, fmt);
        n = vsnprintf((char*)s.c_str(), size, fmt.c_str(), marker);
        va_end(marker);
        if ((n>0) && ((b=(n<size))==true)) s.resize(n); else size*=2;
    }
}

//=============================================================================
void spfa(std::string &s, const std::string fmt, ...)
{
    std::string ss;
    int n, size=100;
    bool b=false;
    va_list marker;

    while (!b)
    {
        ss.resize(size);
        va_start(marker, fmt);
        n = vsnprintf((char*)ss.c_str(), size, fmt.c_str(), marker);
        va_end(marker);
        if ((n>0) && ((b=(n<size))==true)) ss.resize(n); else size*=2;
    }
    s += ss;
}

[previous answer]
A very late answer, but for those who, like me, do like the \'sprintf\'-way: I\'ve written and are using the following functions. If you like it, you can expand the %-options to more closely fit the sprintf ones; the ones in there currently are sufficient for my needs. You use stringf() and stringfappend() same as you would sprintf. Just remember that the parameters for ... must be POD types.

//=============================================================================
void DoFormatting(std::string& sF, const char* sformat, va_list marker)
{
    char *s, ch=0;
    int n, i=0, m;
    long l;
    double d;
    std::string sf = sformat;
    std::stringstream ss;

    m = sf.length();
    while (i<m)
    {
        ch = sf.at(i);
        if (ch == \'%\')
        {
            i++;
            if (i<m)
            {
                ch = sf.at(i);
                switch(ch)
                {
                    case \'s\': { s = va_arg(marker, char*);  ss << s;         } break;
                    case \'c\': { n = va_arg(marker, int);    ss << (char)n;   } break;
                    case \'d\': { n = va_arg(marker, int);    ss << (int)n;    } break;
                    case \'l\': { l = va_arg(marker, long);   ss << (long)l;   } break;
                    case \'f\': { d = va_arg(marker, double); ss << (float)d;  } break;
                    case \'e\': { d = va_arg(marker, double); ss << (double)d; } break;
                    case \'X\':
                    case \'x\':
                        {
                            if (++i<m)
                            {
                                ss << std::hex << std::setiosflags (std::ios_base::showbase);
                                if (ch == \'X\') ss << std::setiosflags (std::ios_base::uppercase);
                                char ch2 = sf.at(i);
                                if (ch2 == \'c\') { n = va_arg(marker, int);  ss << std::hex << (char)n; }
                                else if (ch2 == \'d\') { n = va_arg(marker, int); ss << std::hex << (int)n; }
                                else if (ch2 == \'l\') { l = va_arg(marker, long);    ss << std::hex << (long)l; }
                                else ss << \'%\' << ch << ch2;
                                ss << std::resetiosflags (std::ios_base::showbase | std::ios_base::uppercase) << std::dec;
                            }
                        } break;
                    case \'%\': { ss << \'%\'; } break;
                    default:
                    {
                        ss << \"%\" << ch;
                        //i = m; //get out of loop
                    }
                }
            }
        }
        else ss << ch;
        i++;
    }
    va_end(marker);
    sF = ss.str();
}

//=============================================================================
void stringf(string& stgt,const char *sformat, ... )
{
    va_list marker;
    va_start(marker, sformat);
    DoFormatting(stgt, sformat, marker);
}

//=============================================================================
void stringfappend(string& stgt,const char *sformat, ... )
{
    string sF = \"\";
    va_list marker;
    va_start(marker, sformat);
    DoFormatting(sF, sformat, marker);
    stgt += sF;
}


回答9:

In order to format std::string in a \'sprintf\' manner, call snprintf (arguments nullptr and 0) to get length of buffer needed. Write your function using C++11 variadic template like this:

#include <cstdio>
#include <string>
#include <cassert>

template< typename... Args >
std::string string_sprintf( const char* format, Args... args ) {
  int length = std::snprintf( nullptr, 0, format, args... );
  assert( length >= 0 );

  char* buf = new char[length + 1];
  std::snprintf( buf, length + 1, format, args... );

  std::string str( buf );
  delete[] buf;
  return std::move(str);
}

Compile with C++11 support, for example in GCC: g++ -std=c++11

Usage:

  std::cout << string_sprintf(\"%g, %g\\n\", 1.23, 0.001);


回答10:

This is how google does it: StringPrintf (BSD License)
and facebook does it in a quite similar fashion: StringPrintf (Apache License)
Both provide with a convenient StringAppendF too.



回答11:

My two cents on this very popular question.

To quote the manpage of printf-like functions:

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte (\'\\0\')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.

In other words, a sane C++11 implementation should be the following:

#include <string>
#include <cstdio>

template <typename... Ts>
std::string fmt (const std::string &fmt, Ts... vs)
{
    char b;
    size_t required = std::snprintf(&b, 0, fmt.c_str(), vs...) + 1;
        // See comments: the +1 is necessary, while the first parameter
        //               can also be set to nullptr

    char bytes[required];
    std::snprintf(bytes, required, fmt.c_str(), vs...);

    return std::string(bytes);
}

It works quite well :)

Variadic templates are supported only in C++11. The answer from pixelpoint show a similar technique using older programming styles.

It\'s weird that C++ does not have such a thing out of the box. They recently added to_string(), which in my opinion is a great step forward. I\'m wondering if they will add a .format operator to the std::string eventually...

Edit

As alexk7 pointed out, A +1 is needed on the return value of std::snprintf, since we need to have space for the \\0 byte. Intuitively, on most architectures missing the +1 will cause the required integer to be partially overwritten with a 0. This will happen after the evaluation of required as actual parameter for std::snprintf, so the effect should not be visible.

This problem could however change, for instance with compiler optimization: what if the compiler decides to use a register for the required variable? This is the kind of errors which sometimes result in security issues.



回答12:

template<typename... Args>
std::string string_format(const char* fmt, Args... args)
{
    size_t size = snprintf(nullptr, 0, fmt, args...);
    std::string buf;
    buf.reserve(size + 1);
    buf.resize(size);
    snprintf(&buf[0], size + 1, fmt, args...);
    return buf;
}

Using C99 snprintf and C++11



回答13:

Based on the answer provided by Erik Aronesty:

std::string string_format(const std::string &fmt, ...) {
    std::vector<char> str(100,\'\\0\');
    va_list ap;
    while (1) {
        va_start(ap, fmt);
        auto n = vsnprintf(str.data(), str.size(), fmt.c_str(), ap);
        va_end(ap);
        if ((n > -1) && (size_t(n) < str.size())) {
            return str.data();
        }
        if (n > -1)
            str.resize( n + 1 );
        else
            str.resize( str.size() * 2);
    }
    return str.data();
}

This avoids the need to cast away const from the result of .c_str() which was in the original answer.



回答14:

inline void format(string& a_string, const char* fmt, ...)
{
    va_list vl;
    va_start(vl, fmt);
    int size = _vscprintf( fmt, vl );
    a_string.resize( ++size );
    vsnprintf_s((char*)a_string.data(), size, _TRUNCATE, fmt, vl);
    va_end(vl);
}


回答15:

You could try this:

string str;
str.resize( _MAX_PATH );

sprintf( &str[0], \"%s %s\", \"hello\", \"world\" );
// optionals
// sprintf_s( &str[0], str.length(), \"%s %s\", \"hello\", \"world\" ); // Microsoft
// #include <stdio.h>
// snprintf( &str[0], str.length(), \"%s %s\", \"hello\", \"world\" ); // c++11

str.resize( strlen( str.data() ) + 1 );


回答16:

If you are on a system that has asprintf(3), you can easily wrap it:

#include <iostream>
#include <cstdarg>
#include <cstdio>

std::string format(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));

std::string format(const char *fmt, ...)
{
    std::string result;

    va_list ap;
    va_start(ap, fmt);

    char *tmp = 0;
    int res = vasprintf(&tmp, fmt, ap);
    va_end(ap);

    if (res != -1) {
        result = tmp;
        free(tmp);
    } else {
        // The vasprintf call failed, either do nothing and
        // fall through (will return empty string) or
        // throw an exception, if your code uses those
    }

    return result;
}

int main(int argc, char *argv[]) {
    std::string username = \"you\";
    std::cout << format(\"Hello %s! %d\", username.c_str(), 123) << std::endl;
    return 0;
}


回答17:

Tested, Production Quality Answer

This answer handles the general case with standards compliant techniques. The same approach is given as an example on CppReference.com near the bottom of their page. Unlike their example, this code fits the question\'s requirements and is field tested in robotics and satellite applications. It also has improved commenting. Design quality is discussed further below.

#include <string>
#include <cstdarg>
#include <vector>

// requires at least C++11
const std::string vformat(const char * const zcFormat, ...) {

    // initialize use of the variable argument array
    va_list vaArgs;
    va_start(vaArgs, zcFormat);

    // reliably acquire the size
    // from a copy of the variable argument array
    // and a functionally reliable call to mock the formatting
    va_list vaArgsCopy;
    va_copy(vaArgsCopy, vaArgs);
    const int iLen = std::vsnprintf(NULL, 0, zcFormat, vaArgsCopy);
    va_end(vaArgsCopy);

    // return a formatted string without risking memory mismanagement
    // and without assuming any compiler or platform specific behavior
    std::vector<char> zc(iLen + 1);
    std::vsnprintf(zc.data(), zc.size(), zcFormat, vaArgs);
    va_end(vaArgs);
    return std::string(zc.data(), iLen); }

#include <ctime>
#include <iostream>
#include <iomanip>

// demonstration of use
int main() {

    std::time_t t = std::time(nullptr);
    std::cerr
        << std::put_time(std::localtime(& t), \"%D %T\")
        << \" [debug]: \"
        << vformat(\"Int 1 is %d, Int 2 is %d, Int 3 is %d\", 11, 22, 33)
        << std::endl;
    return 0; }

Predictable Linear Efficiency

Two passes are necessities for a secure, reliable, and predictable reusable function per the question specifications. Presumptions about the distribution of sizes of vargs in a reusable function is bad programming style and should be avoided. In this case, arbitrarily large variable length representations of vargs is a key factor in choice of algorithm.

Retrying upon overflow is exponentially inefficient, which is another reason discussed when the C++11 standards committee discussed the above proposal to provide a dry run when the write buffer is null.

In the above production ready implementation, the first run is such a dry run to determine allocation size. No allocation occurs. Parsing of printf directives and the reading of vargs has been made extremely efficient over decades. Reusable code should be predictable, even if a small inefficiency for trivial cases must be sacrificed.

Security and Reliability

Andrew Koenig said to a small group of us after his lecture at a Cambridge event, \"User functions shouldn\'t rely on the exploitation of a failure for unexceptional functionality.\" As usual, his wisdom has been shown true in the record since. Fixed and closed security bug issues often indicate retry hacks in the description of the hole exploited prior to the fix.

This is mentioned in the formal standards revision proposal for the null buffer feature in Alternative to sprintf, C9X Revision Proposal, ISO IEC Document WG14 N645/X3J11 96-008. An arbitrarily long string inserted per print directive, \"%s,\" within the constraints of dynamic memory availability, is not an exception, and should not be exploited to produce, \"Unexceptional functionality.\"

Consider the proposal along side the example code given at the bottom of the C++Reference.org page linked to in the first paragraph of this answer.

Also, the testing of failure cases is rarely as robust of success cases.

Portability

All major O.S. vendors provide compilers that fully support std::vsnprintf as part of the c++11 standards. Hosts running products of vendors that no longer maintain distributions should be furnished with g++ or clang++ for many reasons.

Stack Use

Stack use in the 1st call to std::vsnprintf will be less than or equal to that of the 2nd, and and it will be freed before the 2nd call begins. If the first call exceeds stack availability, then std::fprintf would fail too.



回答18:

string doesn\'t have what you need, but std::stringstream does. Use a stringstream to create the string and then extract the string. Here is a comprehensive list on the things you can do. For example:

cout.setprecision(10); //stringstream is a stream like cout

will give you 10 decimal places of precision when printing a double or float.



回答19:

This is the code I use to do this in my program... It\'s nothing fancy, but it does the trick... Note, you will have to adjust your size as applicable. MAX_BUFFER for me is 1024.

std::string Format ( const char *fmt, ... )
{
    char textString[MAX_BUFFER*5] = {\'\\0\'};

    // -- Empty the buffer properly to ensure no leaks.
    memset(textString, \'\\0\', sizeof(textString));

    va_list args;
    va_start ( args, fmt );
    vsnprintf ( textString, MAX_BUFFER*5, fmt, args );
    va_end ( args );
    std::string retStr = textString;
    return retStr;
}


回答20:

Took the idea from Dacav and pixelpoint\'s answer. I played around a bit and got this:

#include <cstdarg>
#include <cstdio>
#include <string>

std::string format(const char* fmt, ...)
{
    va_list vl;

    va_start(vl, fmt);
    int size = vsnprintf(0, 0, fmt, vl) + sizeof(\'\\0\');
    va_end(vl);

    char buffer[size];

    va_start(vl, fmt);
    size = vsnprintf(buffer, size, fmt, vl);
    va_end(vl);

    return std::string(buffer, size);
}

With sane programming practice I believe the code should be enough, however I\'m still open to more secure alternatives that are still simple enough and would not require C++11.


And here\'s another version that makes use of an initial buffer to prevent second call to vsnprintf() when initial buffer is already enough.

std::string format(const char* fmt, ...)
{

    va_list vl;
    int size;

    enum { INITIAL_BUFFER_SIZE = 512 };

    {
        char buffer[INITIAL_BUFFER_SIZE];

        va_start(vl, fmt);
        size = vsnprintf(buffer, INITIAL_BUFFER_SIZE, fmt, vl);
        va_end(vl);

        if (size < INITIAL_BUFFER_SIZE)
            return std::string(buffer, size);
    }

    size += sizeof(\'\\0\');

    char buffer[size];

    va_start(vl, fmt);
    size = vsnprintf(buffer, size, fmt, vl);
    va_end(vl);

    return std::string(buffer, size);
}

(It turns out that this version is just similar to Piti Ongmongkolkul\'s answer, only that it doesn\'t use new and delete[], and also specifies a size when creating std::string.

The idea here of not using new and delete[] is to imply usage of the stack over the heap since it doesn\'t need to call allocation and deallocation functions, however if not properly used, it could be dangerous to buffer overflows in some (perhaps old, or perhaps just vulnerable) systems. If this is a concern, I highly suggest using new and delete[] instead. Note that the only concern here is about the allocations as vsnprintf() is already called with limits, so specifying a limit based on the size allocated on the second buffer would also prevent those.)



回答21:

I usually use this:

std::string myformat(const char *const fmt, ...)
{
        char *buffer = NULL;
        va_list ap;

        va_start(ap, fmt);
        (void)vasprintf(&buffer, fmt, ap);
        va_end(ap);

        std::string result = buffer;
        free(buffer);

        return result;
}

Disadvantage: not all systems support vasprint



回答22:

Below slightly modified version of @iFreilicht answer, updated to C++14 (usage of make_unique function instead of raw declaration) and added support for std::string arguments (based on Kenny Kerr article)

#include <iostream>
#include <memory>
#include <string>
#include <cstdio>

template <typename T>
T process_arg(T value) noexcept
{
    return value;
}

template <typename T>
T const * process_arg(std::basic_string<T> const & value) noexcept
{
    return value.c_str();
}

template<typename ... Args>
std::string string_format(const std::string& format, Args const & ... args)
{
    const auto fmt = format.c_str();
    const size_t size = std::snprintf(nullptr, 0, fmt, process_arg(args) ...) + 1;
    auto buf = std::make_unique<char[]>(size);
    std::snprintf(buf.get(), size, fmt, process_arg(args) ...);
    auto res = std::string(buf.get(), buf.get() + size - 1);
    return res;
}

int main()
{
    int i = 3;
    float f = 5.f;
    char* s0 = \"hello\";
    std::string s1 = \"world\";
    std::cout << string_format(\"i=%d, f=%f, s=%s %s\", i, f, s0, s1) << \"\\n\";
}

Output:

i = 3, f = 5.000000, s = hello world

Feel free to merge this answer with the original one if desired.



回答23:

Very-very simple solution.

std::string strBuf;
strBuf.resize(256);
int iCharsPrinted = sprintf_s((char *)strPath.c_str(), strPath.size(), ...);
strBuf.resize(iCharsPrinted);


回答24:

One solution I\'ve favoured is to do this with sprintf directly into the std::string buffer, after making said buffer big enough:

#include <string>
#include <iostream>

using namespace std;

string l_output;
l_output.resize(100);

for (int i = 0; i < 1000; ++i)
{       
    memset (&l_output[0], 0, 100);
    sprintf (&l_output[0], \"\\r%i\\0\", i);

    cout << l_output;
    cout.flush();
}

So, create the std::string, resize it, access its buffer directly...



回答25:

Poco Foundation library has a very convenient format function, which supports std::string in both the format string and the values:

  • Doc: http://pocoproject.org/docs/Poco.html#7308
  • Source: https://github.com/pocoproject/poco/blob/develop/Foundation/src/Format.cpp


回答26:

You can format C++ output in cout using iomanip header file. Make sure that you include iomanip header file before you use any of the helper functions like setprecision, setfill etc.

Here is a code snippet I have used in the past to print the average waiting time in the vector, which I have \"accumulated\".

#include<iomanip>
#include<iostream>
#include<vector>
#include<numeric>

...

cout<< \"Average waiting times for tasks is \" << setprecision(4) << accumulate(all(waitingTimes), 0)/double(waitingTimes.size()) ;
cout << \" and \" << Q.size() << \" tasks remaining\" << endl;

Here is a brief description of how we can format C++ streams. http://www.cprogramming.com/tutorial/iomanip.html



回答27:

There can be problems, if the buffer is not large enough to print the string. You must determine the length of the formatted string before printing a formatted message in there. I make own helper to this (tested on Windows and Linux GCC), and you can try use it.

String.cpp: http://pastebin.com/DnfvzyKP
String.h: http://pastebin.com/7U6iCUMa

String.cpp:

#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <string>

using ::std::string;

#pragma warning(disable : 4996)

#ifndef va_copy
#ifdef _MSC_VER
#define va_copy(dst, src) dst=src
#elif !(__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
#define va_copy(dst, src) memcpy((void*)dst, (void*)src, sizeof(*src))
#endif
#endif

///
/// \\breif Format message
/// \\param dst String to store formatted message
/// \\param format Format of message
/// \\param ap Variable argument list
///
void toString(string &dst, const char *format, va_list ap) throw() {
  int length;
  va_list apStrLen;
  va_copy(apStrLen, ap);
  length = vsnprintf(NULL, 0, format, apStrLen);
  va_end(apStrLen);
  if (length > 0) {
    dst.resize(length);
    vsnprintf((char *)dst.data(), dst.size() + 1, format, ap);
  } else {
    dst = \"Format error! format: \";
    dst.append(format);
  }
}

///
/// \\breif Format message
/// \\param dst String to store formatted message
/// \\param format Format of message
/// \\param ... Variable argument list
///
void toString(string &dst, const char *format, ...) throw() {
  va_list ap;
  va_start(ap, format);
  toString(dst, format, ap);
  va_end(ap);
}

///
/// \\breif Format message
/// \\param format Format of message
/// \\param ... Variable argument list
///
string toString(const char *format, ...) throw() {
  string dst;
  va_list ap;
  va_start(ap, format);
  toString(dst, format, ap);
  va_end(ap);
  return dst;
}

///
/// \\breif Format message
/// \\param format Format of message
/// \\param ap Variable argument list
///
string toString(const char *format, va_list ap) throw() {
  string dst;
  toString(dst, format, ap);
  return dst;
}


int main() {
  int a = 32;
  const char * str = \"This works!\";

  string test(toString(\"\\nSome testing: a = %d, %s\\n\", a, str));
  printf(test.c_str());

  a = 0x7fffffff;
  test = toString(\"\\nMore testing: a = %d, %s\\n\", a, \"This works too..\");
  printf(test.c_str());

  a = 0x80000000;
  toString(test, \"\\nMore testing: a = %d, %s\\n\", a, \"This way is cheaper\");
  printf(test.c_str());

  return 0;
}

String.h:

#pragma once
#include <cstdarg>
#include <string>

using ::std::string;

///
/// \\breif Format message
/// \\param dst String to store formatted message
/// \\param format Format of message
/// \\param ap Variable argument list
///
void toString(string &dst, const char *format, va_list ap) throw();
///
/// \\breif Format message
/// \\param dst String to store formatted message
/// \\param format Format of message
/// \\param ... Variable argument list
///
void toString(string &dst, const char *format, ...) throw();
///
/// \\breif Format message
/// \\param format Format of message
/// \\param ... Variable argument list
///
string toString(const char *format, ...) throw();

///
/// \\breif Format message
/// \\param format Format of message
/// \\param ap Variable argument list
///
string toString(const char *format, va_list ap) throw();


回答28:

I gave it a try, with regular expressions. I implemented it for ints and const strings as an example, but you can add whatever other types (POD types but with pointers you can print anything).

#include <assert.h>
#include <cstdarg>

#include <string>
#include <sstream>
#include <regex>

static std::string
formatArg(std::string argDescr, va_list args) {
    std::stringstream ss;
    if (argDescr == \"i\") {
        int val = va_arg(args, int);
        ss << val;
        return ss.str();
    }
    if (argDescr == \"s\") {
        const char *val = va_arg(args, const char*);
        ss << val;
        return ss.str();
    }
    assert(0); //Not implemented
}

std::string format(std::string fmt, ...) {
    std::string result(fmt);
    va_list args;
    va_start(args, fmt);
    std::regex e(\"\\\\{([^\\\\{\\\\}]+)\\\\}\");
    std::smatch m;
    while (std::regex_search(fmt, m, e)) {
        std::string formattedArg = formatArg(m[1].str(), args);
        fmt.replace(m.position(), m.length(), formattedArg);
    }
    va_end(args);
    return fmt;
}

Here is an example of use of it:

std::string formatted = format(\"I am {s} and I have {i} cats\", \"bob\", 3);
std::cout << formatted << std::endl;

Output:

I am bob and I have 3 cats



回答29:

this can be tried out. simple. really does not use nuances of the string class though.

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <string>
#include <exception>
using namespace std;

//---------------------------------------------------------------------

class StringFormatter
{
public:
    static string format(const char *format, ...);
};

string StringFormatter::format(const char *format, ...)
{
    va_list  argptr;

    va_start(argptr, format);

        char   *ptr;
        size_t  size;
        FILE   *fp_mem = open_memstream(&ptr, &size);
        assert(fp_mem);

        vfprintf (fp_mem, format, argptr);
        fclose (fp_mem);

    va_end(argptr);

    string ret = ptr;
    free(ptr);

    return ret;
}

//---------------------------------------------------------------------

int main(void)
{
    string temp = StringFormatter::format(\"my age is %d\", 100);
    printf(\"%s\\n\", temp.c_str());

    return 0;
}


回答30:

_return.desc = (boost::format(\"fail to detect. cv_result = %d\") % st_result).str();