I want to create a std::ifstream using std::string - c++
This is a discussion on I want to create a std::ifstream using std::string - c++ ; { 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 ...
-
I want to create a std::ifstream using std::string
{ 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! ]
-
Re: I want to create a std::ifstream using std::string
On Fri, 27 Apr 2007, gidaeyeo wrote:
> 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! ]
-
Re: I want to create a std::ifstream using std::string
On Apr 27, 11:53 pm, Sebastian Redl <e0226...@stud3.tuwien.ac.at>
wrote:
> On Fri, 27 Apr 2007, gidaeyeo wrote:
....
> > 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.
>
....
> 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:
streamstring 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! ]
-
Similar Threads
-
By Application Development in forum c++
Replies: 4
Last Post: 12-18-2007, 04:07 AM
-
By Application Development in forum CSharp
Replies: 2
Last Post: 11-30-2007, 02:50 PM
-
By Application Development in forum labview
Replies: 0
Last Post: 10-24-2007, 11:40 AM
-
By Application Development in forum c++
Replies: 4
Last Post: 07-12-2007, 02:02 PM