473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sstream confusion

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::ostringstream 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
>get_back >j;
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 ?)
Sep 15 '07 #1
4 1533
arnuld wrote:
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::ostringstream 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
>get_back >j;
format_input >get_back >get_back >i
// "i" "=" 512
>get_back >get_back >j;
// "j" "=" 400
in your code, after ">i", the "format_input" is not in good state

--
Thanks
Barry
Sep 15 '07 #2
arnuld wrote:
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::ostringstream 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 Tstd::string toString( T t ) {
std::ostringstream out;
out << t;
return out.str();
}

-
-
Ian Collins.
Sep 15 '07 #3
arnuld wrote:
....
int main()
{
int i = 512;
int j = 400;

std::ostringstream 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::ostringstream 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.
Sep 15 '07 #4
arnuld wrote:
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::ostringstream 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
>get_back >j;

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
Sep 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
0
by: i_have_control | last post by:
I'd be grateful for any input on this one: I have three web domains. The destinations of two are set to folders on the first, though that fact is transparent to the user (i.e: it does not...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
2
by: Praveen Chandra | last post by:
Hi, I am trying to build a static library, while building the library i am getting the following error: fatal error C1189: #error : "No sstream/strstream implementation" Following is the...
5
by: bpazolli | last post by:
Quick noob question. I have dev-C++ 4 doing a tutorial. Typed in #include <sstreamand get file does not exist. Thanks, Ben Pazolli
1
by: gowrinm | last post by:
Hi, I am getting build error when I used aCC compiler for compiling C++ code with <sstream> I don't know what is missing here. Should I have to link any standard libraries / Include PATH? I...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.