std::string formatting like sprintf

前端 未结 30 3089
野趣味
野趣味 2020-11-22 04:42

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

30条回答
  •  暖寄归人
    2020-11-22 05:17

    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 
    // snprintf( &str[0], str.length(), "%s %s", "hello", "world" ); // c++11
    
    str.resize( strlen( str.data() ) + 1 );
    

提交回复
热议问题