how can i convert a file stream in a string?

Discussion in 'C++' started by Manuel, Sep 12, 2006.

  1. Manuel

    Manuel Guest

    Hi, I need to convert a text file in a string or char*. exists some
    function that does it? or how can i do it?

    Thanks,
    bye
     
    Manuel, Sep 12, 2006
    #1
    1. Advertising

  2. Manuel

    Noah Roberts Guest

    Manuel wrote:
    > Hi, I need to convert a text file in a string or char*. exists some
    > function that does it? or how can i do it?


    I don't understand the question.
     
    Noah Roberts, Sep 12, 2006
    #2
    1. Advertising

  3. Manuel

    Kai-Uwe Bux Guest

    Manuel wrote:

    > Hi, I need to convert a text file in a string or char*. exists some
    > function that does it? or how can i do it?


    To initialize a string from a file, you can do:

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iterator>

    int main ( void ) {
    std::ifstream in_file ( "data.txt" );
    std::string the_str ( std::istreambuf_iterator<char>( in_file ),
    (std::istreambuf_iterator<char>()) );
    std::cout << the_str;
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 12, 2006
    #3
  4. Manuel

    Nate Barney Guest

    Manuel wrote:
    > Hi, I need to convert a text file in a string or char*. exists some
    > function that does it? or how can i do it?


    If you're talking about copying the contents of a istream into a string,
    then one way to do it is to use an ostringstream. You can insert the
    streambuf pointer from the istream into the ostringstream and then get
    the string from that. For example:

    #include <fstream>
    #include <string>
    #include <sstream>

    int main()
    {
    std::ifstream in("textfile.txt");
    std::eek:stringstream out;

    out << in.rdbuf();

    std::string s = out.str();

    return 0;
    }

    Hope this helps,
    Nate
     
    Nate Barney, Sep 12, 2006
    #4
  5. Manuel

    Nate Barney Guest

    Kai-Uwe Bux wrote:
    >
    > To initialize a string from a file, you can do:
    >
    > [ snip std::istreambuf_iterator explanation ]


    This of course is better than my solution. Thanks for reminding me
    about this.

    Nate
     
    Nate Barney, Sep 12, 2006
    #5
  6. Manuel

    Manuel Guest

    First, thank you very much by your answers.

    I decide to use the solution of Nate because it is more simple and
    clearly and it make exactly as i need it.

    But, why is kai-uwe's solution better than Nate's solution?

    best regard,
    Manuel
     
    Manuel, Sep 12, 2006
    #6
  7. Manuel

    Bart Guest

    Manuel wrote:
    > First, thank you very much by your answers.
    >
    > I decide to use the solution of Nate because it is more simple and
    > clearly and it make exactly as i need it.
    >
    > But, why is kai-uwe's solution better than Nate's solution?


    Because it achieves the same without using an additional ostringstream
    object. It's also a question of style. Some people just love using
    iterators for everything. IMHO use whatever is clearer to you.

    Regards,
    Bart.
     
    Bart, Sep 12, 2006
    #7
  8. Manuel

    Default User Guest

    Bart wrote:

    > Manuel wrote:
    > > First, thank you very much by your answers.
    > >
    > > I decide to use the solution of Nate because it is more simple and
    > > clearly and it make exactly as i need it.
    > >
    > > But, why is kai-uwe's solution better than Nate's solution?

    >
    > Because it achieves the same without using an additional ostringstream
    > object. It's also a question of style. Some people just love using
    > iterators for everything. IMHO use whatever is clearer to you.


    Interestingly, it didn't compile under VC++ 6. Looks like the iterator
    constructor that takes an istream isn't implemented.




    Brian
     
    Default User, Sep 12, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Casper Bang

    Convert string to stream?

    Casper Bang, Sep 25, 2003, in forum: C++
    Replies:
    2
    Views:
    911
    Jerry Coffin
    Sep 26, 2003
  2. Rasmusson, Lars
    Replies:
    1
    Views:
    760
    popov
    Apr 30, 2004
  3. Titus Barik

    convert string to input stream

    Titus Barik, Aug 31, 2004, in forum: Python
    Replies:
    2
    Views:
    2,942
    Alex Martelli
    Sep 1, 2004
  4. Replies:
    9
    Views:
    641
    Alex Buell
    Apr 27, 2006
  5. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    456
    John Harrison
    Feb 17, 2007
Loading...

Share This Page