sstream confusion

A

arnuld

I am tryign to understand the sstream class but got confused by its
aspects. here is the example code:

/* C++ Primer - 4/e
*
* Example from Section 8.5 - sstream
*
*/


#include <iostream>
#include <sstream>

int main()
{
int i = 512;
int j = 400;

std::eek:stringstream format_int;

std::cout << "changing ints to strings\n" << std::endl;

format_int << "i = " << i << "\n"
<< "j = " << j << "\n";

std::cout << "done.... printing output..\n\n"
<< format_int
<< std::endl;

/* changing back to int */
std::cout << "changing back to int" << "\n\n"; std::istringstream
format_input(format_int.str());

std::string get_back;
format_input >> get_back >> i
std::cout << i
<< "\t"
<< j
<< std::endl;

return 0;
}


--------- OUTPUT --------------
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra 8.5.cpp
~/programming/c++ $ ./a.out
changing ints to strings

done.... printing output..

0x7fff863f3058
changing back to int

512 400
~/programming/c++ $


I do not get the output, the int vales are not converted to strings, i
just got a memory address. I do not even understand the mechanics of
"format_int" here, Where exactly that thing exists ? (I mean, fstream is
used to handle files which are stored oon hard-disk and cin and cout
handle the terminal IO. What exactly that format_int handles ?)
 
K

Kai-Uwe Bux

arnuld said:
I am tryign to understand the sstream class but got confused by its
aspects. here is the example code:

/* C++ Primer - 4/e
*
* Example from Section 8.5 - sstream
*
*/


#include <iostream>
#include <sstream>

int main()
{
int i = 512;
int j = 400;

std::eek:stringstream format_int;

std::cout << "changing ints to strings\n" << std::endl;

format_int << "i = " << i << "\n"
<< "j = " << j << "\n";

std::cout << "done.... printing output..\n\n"
<< format_int

make that:

<< format_int.str()
<< std::endl;

/* changing back to int */
std::cout << "changing back to int" << "\n\n"; std::istringstream
format_input(format_int.str());

std::string get_back;
format_input >> get_back >> i

std::cout << i
<< "\t"
<< j
<< std::endl;

return 0;
}


--------- OUTPUT --------------
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra 8.5.cpp
~/programming/c++ $ ./a.out
changing ints to strings

done.... printing output..

0x7fff863f3058
changing back to int

512 400
~/programming/c++ $


I do not get the output, the int vales are not converted to strings, i
just got a memory address.

You hit a conversion, probably something like format_int --> void*. Like
other streams, stringstreams convert to void* so that one can have these
typical idioms

while ( stream >> var ) {
}
I do not even understand the mechanics of
"format_int" here, Where exactly that thing exists ? (I mean, fstream is
used to handle files which are stored oon hard-disk and cin and cout
handle the terminal IO. What exactly that format_int handles ?)

stringstreams have a buffer that resides in memory, to which access is
offered through the str() method.


Best

Kai-Uwe Bux
 
G

Gianni Mariani

arnuld wrote:
....
int main()
{
int i = 512;
int j = 400;

std::eek:stringstream format_int;

std::cout << "changing ints to strings\n" << std::endl;

format_int << "i = " << i << "\n"
<< "j = " << j << "\n";

std::cout << "done.... printing output..\n\n"
<< format_int

try
<< format_int.str()
<< std::endl; ....

I do not get the output, the int vales are not converted to strings, i
just got a memory address.

It appears that was due to trying to print a ostringstream and not the
contained string.
... I do not even understand the mechanics of
"format_int" here, Where exactly that thing exists ?

Where you define it - i.e.
std::eek:stringstream format_int;

A local variable.
... (I mean, fstream is
used to handle files which are stored oon hard-disk and cin and cout
handle the terminal IO. What exactly that format_int handles ?)

Writing to memory. As you write to it, it appends to an internal buffer
which you can later retrieve by calling the "str()" member function.
 
B

Barry

arnuld said:
I am tryign to understand the sstream class but got confused by its
aspects. here is the example code:

/* C++ Primer - 4/e
*
* Example from Section 8.5 - sstream
*
*/


#include <iostream>
#include <sstream>

int main()
{
int i = 512;
int j = 400;

std::eek:stringstream format_int;

std::cout << "changing ints to strings\n" << std::endl;

format_int << "i = " << i << "\n"
<< "j = " << j << "\n";

std::cout << "done.... printing output..\n\n"
<< format_int
<< std::endl;

/* changing back to int */
std::cout << "changing back to int" << "\n\n"; std::istringstream

take a look at "format_int.str()", now it should be
i = 512
j = 400

as there are ' ' between around '='

format_input(format_int.str());

std::string get_back;
format_input >> get_back >> i

format_input >> get_back >> get_back >> i
// "i" "=" 512 // "j" "=" 400


in your code, after ">> i", the "format_input" is not in good state
 
I

Ian Collins

arnuld said:
I am tryign to understand the sstream class but got confused by its
aspects. here is the example code:

/* C++ Primer - 4/e
*
* Example from Section 8.5 - sstream
*
*/


#include <iostream>
#include <sstream>

int main()
{
int i = 512;
int j = 400;

std::eek:stringstream format_int;

std::cout << "changing ints to strings\n" << std::endl;

format_int << "i = " << i << "\n"
<< "j = " << j << "\n";

std::cout << "done.... printing output..\n\n"
<< format_int

This invokes some conversion operator or another on format_int you
probably want format_int.str().
I do not get the output, the int vales are not converted to strings, i
just got a memory address. I do not even understand the mechanics of
"format_int" here, Where exactly that thing exists ? (I mean, fstream is
used to handle files which are stored oon hard-disk and cin and cout
handle the terminal IO. What exactly that format_int handles ?)

stringstreams are iostreams that provaide formatted read and write to
text buffers, just as fstreams read and write files.

A common use you will see is converting numeric types to and from
strings, something like

template <typename T> std::string toString( T t ) {
std::eek:stringstream out;
out << t;
return out.str();
}

-
-
Ian Collins.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top