How do i copy an entire file into a string

Discussion in 'C++' started by owolablo, Oct 11, 2006.

  1. owolablo

    owolablo Guest

    I'm trying to make a subprogram,
    Code:

    void file2String(istream& in, string& theFile)


    that can read an entire file into a string variable, including line
    breaks and i have no idea what im doing. I'm using the GNU g++
    compiler on Fedora 4 Linux Distribution.
    Thanks in advance for the help
     
    owolablo, Oct 11, 2006
    #1
    1. Advertising

  2. owolablo wrote:
    > I'm trying to make a subprogram,
    > Code:
    >
    > void file2String(istream& in, string& theFile)
    >
    >
    > that can read an entire file into a string variable, including line
    > breaks and i have no idea what im doing.


    Use 'read' member of 'istream' and keep appending what you read to
    the string, until an error or end-of-file.

    > I'm using the GNU g++
    > compiler on Fedora 4 Linux Distribution.


    We don't care.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 11, 2006
    #2
    1. Advertising

  3. On Wed, 11 Oct 2006 08:59:48 -0400, "Victor Bazarov"
    <> wrote:
    >Use 'read' member of 'istream' and keep appending what you read to
    >the string, until an error or end-of-file.


    You can also reserve enough space to read the entire file with one
    dynamcic allocation (but keep std::string as out param and check the
    return values):
    http://www.cplusplus.com/ref/iostream/istream/tellg.html

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Oct 11, 2006
    #3
  4. owolablo

    Kai-Uwe Bux Guest

    owolablo wrote:

    > I'm trying to make a subprogram,
    > Code:
    >
    > void file2String(istream& in, string& theFile)
    >
    >
    > that can read an entire file into a string variable, including line
    > breaks and i have no idea what im doing.


    Try this:

    #include <iterator>

    void file2string ( std::istream & in, std::string & str ) {
    std::string dummy ( std::istreambuf_iterator<char>( in ),
    (std::istreambuf_iterator<char>()) );
    str.swap( dummy );
    }



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 11, 2006
    #4
  5. owolablo

    Jens Theisen Guest

    "owolablo" <> writes:

    > void file2String(istream& in, string& theFile)
    >
    > that can read an entire file into a string variable, including line
    > breaks and i have no idea what im doing. I'm using the GNU g++
    > compiler on Fedora 4 Linux Distribution.
    > Thanks in advance for the help


    in << ifstream(theFile).rdbuf();

    To read it into a string make `in' be a stringstream.

    --
    Gruß, Jens
     
    Jens Theisen, Oct 11, 2006
    #5
    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. Alex
    Replies:
    2
    Views:
    1,240
  2. Jared Wiltshire
    Replies:
    8
    Views:
    421
    Ian Collins
    Aug 7, 2006
  3. Replies:
    26
    Views:
    2,124
    Roland Pibinger
    Sep 1, 2006
  4. Phil Johnson
    Replies:
    3
    Views:
    427
    Phil Johnson
    Apr 25, 2008
  5. David Southwell
    Replies:
    3
    Views:
    154
    Phrogz
    Nov 4, 2009
Loading...

Share This Page