Discussion:
I want to create a std::ifstream using std::string
(too old to reply)
gidaeyeo
2007-04-27 13:12:13 UTC
Permalink
{ Note: this article was multi-posted to clc++. -mod }

Hi, all.

Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)

i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.

(^^;; I can solve this using making temporty file and write the str on
the file and reading it using std::ifstream...
but I don't want to make a additional file io ^^)

plz show me the way~
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Sebastian Redl
2007-04-27 21:53:47 UTC
Permalink
Post by gidaeyeo
Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)
i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.
Not an ifstream. As the name (cryptically) says, ifstream is a stream for
files.

However, there's a string stream too.

#include <sstream>

std::istringstream iss(str);

You can use it exactly like the file stream, and if you'r always taking
std::istream references you can even use the same code.

Sebastian Redl
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
s5n
2007-05-02 18:05:29 UTC
Permalink
...
Post by Sebastian Redl
Post by gidaeyeo
i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.
Not an ifstream. As the name (cryptically) says, ifstream is a stream for
files.
However, there's a string stream too.
...
Post by Sebastian Redl
std::istringstream iss(str);
You can use it exactly like the file stream, and if you'r always taking
std::istream references you can even use the same code.
To expand on that a bit, here's an example which will let you flexibly
populate your input string:

std::ostreamstring os;
os << "Arbitrary input.";
std::istringstream is( os.str() );

now read from 'is' as you would any std istream.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Seungbeom Kim
2007-05-02 22:11:41 UTC
Permalink
Post by s5n
Post by Sebastian Redl
std::istringstream iss(str);
You can use it exactly like the file stream, and if you'r always taking
std::istream references you can even use the same code.
To expand on that a bit, here's an example which will let you flexibly
std::ostreamstring os;
You meant std::ostringstream here.
Post by s5n
os << "Arbitrary input.";
std::istringstream is( os.str() );
now read from 'is' as you would any std istream.
Why bother with two string streams? Just use std::stringstream:

std::stringstream ss;
ss << "2 3 5 7 11";
int i;
while (ss >> i) std::cout << i << '\n';
--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Continue reading on narkive:
Loading...