f



How to convert std::string to std::istream?

I have a function that use std::istream as parameter. Right now I need
to pass a std::string into it, how can I convert std::string to
std::istream type?

Thanks

Water Lin

-- 
Water Lin's notes and pencils: http://en.waterlin.org
Email: WaterLin@ymail.com
0
WaterLin (10)
11/13/2009 3:20:29 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
10483 Views

Similar Articles

[PageSpeed] 54

Water Lin wrote:
> I have a function that use std::istream as parameter. Right now I need
> to pass a std::string into it, how can I convert std::string to
> std::istream type?

You can't directly, but you can use a stringstream:

#include <iostream>
#include <sstream>

void f( std::istream& in )
{
   std::string s;
   in >> s;
   std::cout << s << std::endl;
}

int main()
{
   std::string s("hello");
   std::istringstream ss( s );

   f( ss );
}

-- 
Ian Collins
0
ian-news (10155)
11/13/2009 4:05:44 AM
Ian Collins <ian-news@hotmail.com> writes:

> Water Lin wrote:
>> I have a function that use std::istream as parameter. Right now I need
>> to pass a std::string into it, how can I convert std::string to
>> std::istream type?
>
> You can't directly, but you can use a stringstream:
>
> #include <iostream>
> #include <sstream>
>
> void f( std::istream& in )
> {
>   std::string s;
>   in >> s;
>   std::cout << s << std::endl;
> }
>
> int main()
> {
>   std::string s("hello");
>   std::istringstream ss( s );
>
>   f( ss );
> }


Thanks, I got it.

Water Lin

-- 
Water Lin's notes and pencils: http://en.waterlin.org
Email: WaterLin@ymail.com
0
WaterLin (10)
11/13/2009 4:47:01 AM
Ian Collins wrote:
> Water Lin wrote:
>> I have a function that use std::istream as parameter. Right now I need
>> to pass a std::string into it, how can I convert std::string to
>> std::istream type?
> 
> You can't directly, but you can use a stringstream:

  Maybe I'm being hugely pedantic here, but what do you mean "you can't
directly"? How is giving the string to a strinstream *not* converting
the string to a std::istream? To me it sounds like being exactly that.
0
nospam270 (2948)
11/13/2009 8:21:37 PM
Reply:

Similar Artilces:

from std::string to std::istream?
Can I convert a std::string to a std::istream object? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);} On 2005-03-17, Gernot Frisch <Me@Privacy.net> wrote: > Can I convert a std::string to a std::istream object? The question doesn't even make sense. What are you trying to do ? Cheers, -- Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/ "Gernot Frisch" <Me@Privacy.net> wrote... > Can I convert a std::string to a std::istream object? No. But you can create an std::istringstream object with it: std::string mystring("1 2 3"); std::istringstream is(mystring); int one, two, three; is >> one >> two >> three; Of course, my effort is wasted if that's not what you wanted. Next time please be more verbose trying to explain what you need instead of asking about legality of a potential solution to your task. V "Victor Bazarov" <v.Abazarov@comAcast.net> schrieb im Newsbeitrag news:nPqdnXLo7cBwMqTfRVn-pQ@comcast.com... > "Gernot Frisch" <Me@Privacy.net> wrote... >> Can I convert a std::string to a std::istream object? > > No. But you can create an std::istringstream object with it: > > std::string mystring("1 2 3"); > std::istringstream is(mystring); > int one, two, three; > is >> one >> two >>...

Q: Convert std::string to std::wstring using std::ctype widen()
Hi All, I've done a little homework (I've read responses to similar from P.J. Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below is what I am performing (Stroustrup's Appendix D recommendation won't compile in Microsoft VC++ 6.0). My question is in reference to MultiByte Character Sets. Will this code perform as expected? I understand every problem has a simple and elegant solution that is wrong. I generally use US English or Unicode, so I don't encounter a lot of issues others may see (a multibyte character using std::string). I have verified it work...

converting std::string to std::wstring
I have a an app that I'm writing which uses char and std::string. I'm using a library which expects wchar_t arrays. Is there a standard way to convert between std::string and std::wstring, or do I need to use something like std::transform()? Thanks "red floyd" <no.spam@here.dude> wrote in message news:I3ejb.539$0g6.117@newssvr29.news.prodigy.com... > I have a an app that I'm writing which uses char and std::string. I'm using > a library which expects wchar_t arrays. > > Is there a standard way to convert between std::string and std::wstring, o...

std::string = char* + std::string
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003 The end result being MyString contans the text " Testing" I'm wondering if this is UB. Apparently MSVC is converting the " " to a std::string, then doing the +. Otherwise it would try to add a std::string to a char* which would fail. I know I can do MyString = std::string(" ") + MyString; and all would still work. But I'm wondering if I have to (to stay within the standards...

Converting from std::wstring to UTF-8 std::string
Hi I'm trying to convert a std::wstring to std::string UTF-8 encoded. I have made a simple function that does the trick for three letters but it can hardly be considered a generic solution. <code> std::string wchar2utf8(std::wstring const& wstr) { std::string str; std::wstring::const_iterator itr =3D wstr.begin(); while(itr !=3D wstr.end()) { switch(*itr) { case 0x00f8: // =F8 str.push_back(0xc3); str.push_back(0xb8); break; case 0x00e5: // =E5 str.push_back(0xc3); str.push_back(0xa5); break; case 0x00e6: // =E6 str.push_back(0xc3); str.push_b...

How to extract an std::string from another std::string?
Suppose I have an std::string called line that has the value "sdsder>abc<gd<< sds". I want to get the substring between the greater- than less-than signs and assign it to another std::string called substr. How do I do this? My guess is that you should use two string iterators, one called it_begin that points to the first occurrence of a greater-than sign and another string iterator called it_end that starts at the same spot as it_begin and goes to the first occurrence of a less-than sign. Then you somehow extract the data between those two iterators. Here's what I have so far: #include <iostream> #include <string> int main() { std::string line = "sdsder>abc<gd<< sds"; std::string::iterator it_begin; std::string::iterator it_end; return 0; } Here's where I'm stumped. What value should I be assigning these two iterators? Obviously their indexes need to be six and ten, but we need to assume that line could be any string at all, not the garbage one given. How do I tell std::string to "find the first occurrence of character such-and-such and point it there"? This is a MIME GnuPG-signed message. If you see this text, it means that your E-mail or Usenet software does not support MIME signed messages. The Internet standard for MIME PGP messages, RFC 2015, was published in 1996. To open this message correctly you will need to install E-mail or Usenet software th...

Extracting an std::string from within another std::string
Suppose I have an std::string called data that I know has at least one greater-than sign and one less-than sign following it. I want to extract data's value to just the letters inside the greater-than and less-than signs. For example, if data = "a c>123<DDR" I want to change data to just "123". How do I do this? I'm trying to assign an std::iterator called it_begin that is set to the first occurrence of a greater-than sign, and another iterator called it_end that is set to the first occurrence of a less-than sign, then call data = data.substr(it_begin,...

std::string::push_back vs. std::string::operator+=
Hi, I just browsed libstdc++6-doc and stumbled over a string operation I haven't noticed before: string::push_back. If for example I want to append single characters to an std::string, which one would be better, or is there any difference at all: mystr.push_back( c ); or mystr += c; Any ideas? I used to use the latter one. Regards, Matthias Matthias K�ppler wrote in news:cntj5d$ooa$03$1@news.t-online.com in comp.lang.c++: > Hi, > > I just browsed libstdc++6-doc and stumbled over a string operation I > haven't noticed before: string::push_back. push_back() is ther...

std::string and std::wostream
Hi, I was wondering why there was no predefine operator<< puting a std::string into a std::wostream. The strangest is that something similar to this would work: std::wostream& operator<<(std::wostream& stream, std::string str) { stream << str.c_str(); return stream; } The only problem with this solution is it invalidate the different iterators you have on the string (because of the call to c_str), while the operator<< should not do that. Thanks, Pierre Pierre Barbier de Reuille wrote: > Hi, > > I was wondering why there was no predefine oper...

std::fstream and std::string
I think the stl is great and I enjoy using it, but there are times when it feels non-elegant, or inconsistent to me. For example, why was fstream.open not written to work directly with a std::string parameter? -Luther --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] Luther Baker wrote: > I think the stl is great and I en...

Solaris Studio C++ std::cout std::string core dump under multithread environment.
Hi, Recently I meet a very strange core dump issue and I investigate this issue for long time and I have some ideas, however I have no idea it is correct or not. So I want some guys could give me some suggestions or tips, thanks a lot. The environment: Solaris X86 64bit, Solaris Studio C++ 12 (Sun Studio C++) with -mt -pthread and libCstd.so, and use libumem library. The core dump point is as follows: // ci is a int, cn and sv is a string // and I am sure ci, cn and sv is only function local variables and they // aren't shared by multithreads cout<<" ...

Convert std::string to std::vector<unsigned char> and vice versa
Hi group, how to convert a string to a vector of unsigned char ? I used to iterate trough the string to set the vector, but I think this is not the best way to do this. I'm a beginner with the STL How about allocating the vector ? should I know before the size ? And I would like to do then opposite conversion, from an unsigned char vector to a string. How to do this ? how about special caracters ? (\n for example) Thanks for your help Best regards, N. * timor.super@gmail.com: > > how to convert a string to a vector of unsigned char ? std::string s = "abra kadabra&...

[NEWBIE] from C string to std::string
Hi guys, which is the best way to define a std::string using some C strings (arrays of chars)? Here it is the code: #include <iostream> #include <string> using namespace std; int main(void) { string s; // this won't even compile, since in this case "this is a" and // others string are interpreted like char arrays // s = "this is a" + " string."; // OK, but awkward s = string("this is a") + string(" string."); cout << s; // a better way?? // s = ... return 0; } -- Stefano Sab...

std::string::npos always < std::string::size() ?
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text; text = "\nThis is a test line with newlines\n<-here and \n<-here and \n\n<-two here"; // Format the text std::string formattedText(text); // Change every occurrance of "\n" to "\r\n" std::string::size_type index = 0; do { index = form...

c-style string vs std::string
I am growing really tired of having to decypher 1000 functions that were written to do simple operations on c-style strings that I could do in 50 lines with streams and std::strings. My peer uses that same old ,"Its more efficient " argument that I always hear. In fact, that argument has grown into ,"we shouldn't use any of the STL containers, because they allocate, which is expensive." For example, I had to debug through 1500 lines today, that simply replaced a token in a char * with another char *, because everything to search for the token, convert characters to dig...

Catching std::strings and c-style strings at once
Hello. Given the following try...catch construct: try { } catch (const string& msg) { } I might be overlooking something basic, but why can't the compiler use one of std::string's constructors in order to match: throw "Something happened!"; The intent is to avoid having to add an extra catch block, i.e.: try { } catch (const string& msg) { } catch (const char* const msg) { } Thank you, -- Ney Andr� de Mello Zunino "Ney Andr� de Mello Zunino" <zunino@inf.ufsc.br> wrote in message news:2vvsg5F2qv3bfU1@uni-berlin.de... > Hello. > > G...

using std::string; string("hello") vs std::string("hello") in header file.
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in code. for example, this is bad practice: header.h #include <string> using std::string; class a{ string x; }; instead, one should write: #include <string> class a{ std::string x; }; The reason given by the author is that 'using std::string' actually pollutes the scope where 'using std::string' is declared. I find this style is rather inconvenient when I want to experimen...

std::string and std::ostringstream performances
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader += "><"; msgHeader += b; msgHeader += "><"; msgHeader += c; msgHeader += ">"; Similarly it uses ostringstream as well and the function that uses this gets called almost on every message that my application gets on the socket. I am using this to precisely construct a XML Message to be sent to another application. What we observed when we ran a c...

Question about std:string and std:cout
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace std; int main() { string s1 = "Hii"; string s2 = "MSDN Fans!\n"; cout<<s1+s2; } --------------------------------- #include <iostream> #include <string> using namespace std; int main() { string s1 = "Hii"; string s2 = "MSDN Fans!\n"; string s3 = s1 + s2; cout<< s3; } Which one would be run faster or they'r totally the same....

std::map<int,std::set<std::string> > Wrong? (Segmentation fault.)
Hello, I have the following code: std::map<int,std::set<std::string> > k; k[0]="1234567890"; k[1]="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error) { std::map<int,std::set<std::string> >::const_iterator i; i=k.find(0); if(i==k.end()) throw std::runtime_error("No zero in k."); return i->second; } Compilation of this code goes well, but I have the following problem while executing this in my implementation: "Segmentation fault". I have pi...

std::vector<char> instead of std::string, where are the string searching functions?
I am working with some legacy code that is in the process of changing to use std::vector<char> instead of a C-style char array. The C-style char array is currently allocated using new char [n]. This array is passed to various C string functions such as strstr, strncmp etc. I need to do the same work but with a std::vector. I googled around for a bit to see if I could find anyone who had already done this work but my search revealed nothing. I wonder if some kind person could point me in the right direction. Now, I realise I could code it all myself but surely there must be som...

Is there systematic performance comparison of std::string and c style string?
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way? On 2007-08-11 16:10, yu_kuo@sina.com wrote: > Is there any comparison data on perfomance difference between > std::string and c style string? Or maybe if there are source code > which could be used to measuer on different compiler/platform, in a > systematic way? Most certainly there is, google is your friend. I believe if you include the word rope in the search you'...

binary write of std::string and std::vector
Hi, I need to make binary write/read of std::string and std::vector to a file (by using ofstream/ifstream). Is there any quick way of doing this, or one has to write/read the contents element by element by using ofstream::write(buffer,size)? L.B. !!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!! *-------------------------------------------------------------------* | Dr. Leslaw Bieniasz, | | Institute of Physical Chemistry of the Polish Academy of Sciences,| | Department of Complex Systems and Chemical ...

std::bitset construction with std::string question
Can anyone tell me why std::bitset<2> foo(std::string("01")) initializes the bitset in reverse order, i.e. foo[0]=true foo[1]=false I would expect the bitset to be initialized in string text order. "Dill Hole" <dillhole@microsoft.com> wrote in message news:7brbgvs98ibjn2fcdtl7drc7tk82fstpoj@4ax.com... > Can anyone tell me why > > std::bitset<2> foo(std::string("01")) > > initializes the bitset in reverse order, i.e. > > foo[0]=true > foo[1]=false > > I would expect the bitset to be initialized in string text or...

Web resources about - How to convert std::string to std::istream? - comp.lang.c++

Resources last updated: 2/18/2016 4:06:54 PM