convert string to ostream

how do I convert a string to an ostream?
arjoshi77Asked:
Who is Participating?
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
jkrCommented:
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
 
HooKooDooKuCommented:
Not so sure about an ostream, but I have used ::CreateStreamOnHGlobal to convert data loaded in a global memory handle into an IStream
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sarabandeCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
Wasn't the question about ostream?
0
 
sarabandeCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
sarabandeCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
No worries, Sara. Thanks.
0
 
arjoshi77Author Commented:
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
 
jkrCommented:
???
0
 
arjoshi77Author Commented:
you too jkr..thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.