How do I use std::stringstream to format data to a string like sprintf "%02d"[modified]
-
I want to use std::stringstream to format data to a string like sprintf "%02d" as below: char szWord[MAX_PATH]; int nValue=3; sprintf(szWord,"%02d",nValue); How do I do that by std::stringstream?
modified on Friday, April 9, 2010 5:17 AM
Check if io stream manipulators helps: http://www.cplusplus.com/reference/iostream/manipulators/[^]
-
I want to use std::stringstream to format data to a string like sprintf "%02d" as below: char szWord[MAX_PATH]; int nValue=3; sprintf(szWord,"%02d",nValue); How do I do that by std::stringstream?
modified on Friday, April 9, 2010 5:17 AM
stringstream does not print, but iostream does. Try the cout object as follows:
int x(3);
std::cout.setfill('0');
std::cout.setw(2);
std::cout << x << endl;//endl causes any following text to goto the a new line.stringstream is used similarly, but as I said before it writes to memory, not the console. Edit: did not pick up on the formating earlier.