LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2005, 04:31 PM   #1
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
c++ stringstream


How can I flush stringstream?
 
Old 06-23-2005, 04:53 PM   #2
bigfez
LQ Newbie
 
Registered: Mar 2005
Posts: 29

Rep: Reputation: 15
Code:
stringstream mystringstream;
mystringstream.flush();
you can also just put an endline into the stream too.

Code:
stringstream mystringstream;
string mymessage;
mystringstream << mymessage << endl;
the endl will flush the stream too.
 
Old 06-23-2005, 04:59 PM   #3
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Well, I tried both even before and it didn't work. Am I missing something else.
I'm trying to do something like that:

Code:
    std::stringstream my_stringstream;
    my_stringstream <<  struct_map->getObject(row, col)->numOccupants();
    switch(i)
      {
      case VILLAGE :
	str = "Village\nOccupants: ";
	str += my_stringstream.str();
	str += "\nRecruits: ";
	my_stringstream.flush();
	my_stringstream <<  struct_map->getObject(row, col)->numRecruits();
	str += my_stringstream.str();
	break;
 
Old 06-23-2005, 05:06 PM   #4
bigfez
LQ Newbie
 
Registered: Mar 2005
Posts: 29

Rep: Reputation: 15
if you are looking for output to stdout you may want to use an ostringstream instead.

I've never used just stringstream but i believe it is used for loading a string buffer.

<edit> never mind, i just read your code more closely</edit>


Last edited by bigfez; 06-23-2005 at 05:08 PM.
 
Old 06-23-2005, 05:15 PM   #5
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
What would be the best way to change int to string?
 
Old 06-23-2005, 11:28 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
This might more easily accomplish what you want to do. String streams are great for pasting together strings (like embedded SQL statements, for example).

Code:
    
    std::stringstream my_stringstream;
    switch(i)
      {
      case VILLAGE :
        my_stringstream.str("");
	my_stringstream 
           << "Village\nOccupants: "
           << struct_map->getObject(row, col)->numOccupants()
	   << "\nRecruits: "
	   <<  struct_map->getObject(row, col)->numRecruits();
        std::cout << myStringStream.str();
	break;
 
Old 06-24-2005, 01:44 AM   #7
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Thanks carl.waldbieser,
I guess I haven't worked enough with streams to figure out that it's so simple.
 
Old 06-24-2005, 07:48 AM   #8
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Possibly worth noting that flush() is used to flush the underlying buffer to the associated device.

Consider a file stream : you would flush() the contents to the file on disk, emptying the internal filebuf.

With a stringstream you are flush() the contents to the string in memory, emptying the internal stringbuf. In all likelihood the internal stringbuf writes directly to this string, effecively making the "buffer" and "device" the same.

Hence, flush() on stringstream doesn't work as you might expect (emptying the underlying string).

Last edited by dakensta; 06-24-2005 at 07:51 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration