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 ...

+ Reply to Thread
Results 1 to 4 of 4

I want to create a std::ifstream using std::string

  1. Default 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! ]


  2. Default 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! ]


  3. Default 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! ]


  4. Default Re: I want to create a std::ifstream using std::string

    s5n wrote:
    > On Apr 27, 11:53 pm, Sebastian Redl <e0226...@stud3.tuwien.ac.at>
    > wrote:
    >> 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;


    You meant std:stringstream here.

    > 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! ]


+ Reply to Thread

Similar Threads

  1. ifstream/string ctor
    By Application Development in forum c++
    Replies: 4
    Last Post: 12-18-2007, 04:07 AM
  2. Create Permutations from Dictionary<string, List<string>>
    By Application Development in forum CSharp
    Replies: 2
    Last Post: 11-30-2007, 02:50 PM
  3. Replies: 0
    Last Post: 10-24-2007, 11:40 AM
  4. ifstream from stdin
    By Application Development in forum c++
    Replies: 4
    Last Post: 07-12-2007, 02:02 PM