Yes or you could do it the C++ way using a string stream. An ostringstream acts exactly like the cout object so if you know how to use one you know how to use both.
-
#include <sstream>
-
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
int main()
-
{
-
long i = 123444788;
-
ostringstream oss;
-
-
oss << i.str();
-
-
string myString = oss.str();
-
-
cout << myString;
-
-
return 0;
-
}
-
In our systems I have started making methods to dump an objects status (mainly for debugging) that takes
ostream&
as an input. ostream is the base class of cout, cerr, clog, ofstream and ostringstream (see
http://www.cplusplus.com/reference/iostream/) and an ostream& can be used exactly as you would use cout. In this way I can send the object dump directly to the screen, directly to a file or into a string for the code to do something else with (transmit over a network for example).
The objects dumping there status do not need to know (on the whole) where the out is going, only that output is required. This is a fine example of the polymorphic nature of C++.