Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert string to ostream

Posted on 2011-10-19
12
Medium Priority
?
2,364 Views
Last Modified: 2012-05-12
how do I convert a string to an ostream?
0
Comment
Question by:arjoshi77
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 36994270
You can write a string to an ostream, convertijng seems a bit off to me. E.g.
void write_string_to_ostream(std::ostream& os, const std::string& str) {

  os << str;
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 36995086
>> how do I convert a string to an ostream?
You can't really convert a string to an ostream. An ostream is an abstract representation of an output stream, so the simplest way to deal with this is to create an ostringstream, which IS_An ostream that represents an in-memory stream buffer.
#include <iostream>
#include <sstream>
#include <string>

void foo(std::ostream & os)
{
   // just to prove the conversion
}

int main()
{
   std::string s;
   std::ostringstream oss(s);
   foo(oss);
}

Open in new window

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 36996300
Not so sure about an ostream, but I have used ::CreateStreamOnHGlobal to convert data loaded in a global memory handle into an IStream
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:sarabande
ID: 36998153
you can "convert" a string to an istringstream  by using the constructor:

std::string str = "xyz 123";
std::istringstream is(str);

Open in new window


that way you can read from istringstream same as you would read from std::cin.

the string "xyz 123" for example could be streamed into a string and an integer:

is >> str >> i;

Open in new window


Sara
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36998239
Wasn't the question about ostream?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 36998326
the answer about "converting" a string to an ostream sufficiently was given by jkr.

i wanted to add the information about istringstream which is derived from istream  and which - contrary to ostringstream - has a constructor that takes a string. so one really could tell it a conversion.

Sara
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36998489
>> the answer about "converting" a string to an ostream sufficiently was given by jkr.
But, that isn't converting that's assigning via a stream operator.

>> and which - contrary to ostringstream - has a constructor that takes a string
Eh? I think you need to look again!

"explicit ostringstream ( const string & str, openmode which = ios_base::out );"
"Additionally, in case that the second constructor version is used, the stream's buffer is initialized with the content of the string object str as if a call to member str."
http://www.cplusplus.com/reference/iostream/ostringstream/ostringstream/

From the C++03 standard:

// 27.7.3.1 Constructors/destructor:
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(const basic_string<charT,traits,Allocator>& str, ios_base::openmode which = ios_base::out);

From the C++11 standard:

// 27.8.3.1 Constructors/destructor:
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(const basic_string<charT,traits,Allocator>& str, ios_base::openmode which = ios_base::out);
basic_ostringstream(basic_ostringstream&& rhs);

>>  so one really could tell it a conversion.
Doesn't that assertion contradict your previous statement, " the answer about "converting" a string to an ostream sufficiently was given by jkr."?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 36998590
thanks for the information. i wasn't aware that std::ostringstream could already be filled by constructor and never used that before (i should have better read your comment, though).

Sara
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36998605
No worries, Sara. Thanks.
0
 

Author Closing Comment

by:arjoshi77
ID: 37000702
thanks evilrix...sara. I just want to express my gratitude for your prompt help. Just to digress a bit....I have undertaken a project using QT+Mysql and as a total learner. The point of itis that by doing so I would get experience/knowledge in both the areas. Over the next weeks I would be needing frequent help ((only after I have tried myself through Google/my own efforts and cannot see a way through). so do continue your support. Appreciate it very much;
0
 
LVL 86

Expert Comment

by:jkr
ID: 37000741
???
0
 

Author Comment

by:arjoshi77
ID: 37010765
you too jkr..thanks for the help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question