Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualNightCreature83

Posted 06 February 2013 - 04:37 PM

The C++ way of doing this is to use a stringstream which works as follows
#include <sstring>
std::stringstream sstring(""); //init to empty string
sstring << "Hello World!" << 1 << 2.0f << 3.0 << "!";
std::string helloWorldString = sstring.str();
The drawback of this for a lot of game developers is that stringstream allocates memory on the fly you do not really want to do that in code that runs often. A sprintf_s in those cases doesn't have this dynamic allocation pattern it will just work on the area you give it through the char buffer. Btw use snprintf or sprintf_s if you can as sprintf will happily write past your buffer if the string to enter is bigger than the provided buffer. These other functions will allow you to pass a maximum number of characters to write into the string and will return -1 if it errors.

#2NightCreature83

Posted 06 February 2013 - 04:36 PM

The C++ way of doing this is to use a stringstream which works as follows
#include <sstring>
std::stringstream sstring(""); //init to empty string
sstring << "Hello World!";
std::string helloWorldString = sstring.str();
The drawback of this for a lot of game developers is that stringstream allocates memory on the fly you do not really want to do that in code that runs often. A sprintf_s in those cases doesn't have this dynamic allocation pattern it will just work on the area you give it through the char buffer. Btw use snprintf or sprintf_s if you can as sprintf will happily write past your buffer if the string to enter is bigger than the provided buffer. These other functions will allow you to pass a maximum number of characters to write into the string and will return -1 if it errors.

#1NightCreature83

Posted 06 February 2013 - 04:36 PM

The C++ way of doing this is to use a stringstream which works as follows
[code=auto:0]
#include <sstring>
std::stringstream sstring(""); //init to empty string
sstring << "Hello World!";
std::string helloWorldString = sstring.str();
[\code]
The drawback of this for a lot of game developers is that stringstream allocates memory on the fly you do not really want to do that in code that runs often. A sprintf_s in those cases doesn't have this dynamic allocation pattern it will just work on the area you give it through the char buffer. Btw use snprintf or sprintf_s if you can as sprintf will happily write past your buffer if the string to enter is bigger than the provided buffer. These other functions will allow you to pass a maximum number of characters to write into the string and will return -1 if it errors.

PARTNERS