Moving text file contents into a std::string

Discussion in 'C++' started by Dave, May 20, 2004.

  1. Dave

    Dave Guest

    Hello all,

    The scheme shown below to move a text file's contents into a std::string
    works with one exception: it drops the carriage return and line feed
    characters. How may I, in a Standard-compliant way, read in a text file's
    contents and keep the carriage returns and line feeds?

    Thanks,
    Dave

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

    using namespace std;

    //
    ****************************************************************************
    *
    //
    ****************************************************************************
    *
    //
    ****************************************************************************
    *
    int main()
    {
    string file_contents;
    ifstream file_stream("test_1.txt");

    copy(
    istream_iterator<char>(file_stream),
    istream_iterator<char>(),
    back_inserter(file_contents)
    );

    cout << file_contents << endl;
    } // main
     
    Dave, May 20, 2004
    #1
    1. Advertising

  2. Dave

    Alan Johnson Guest

    Dave wrote:
    > Hello all,
    >
    > The scheme shown below to move a text file's contents into a std::string
    > works with one exception: it drops the carriage return and line feed
    > characters. How may I, in a Standard-compliant way, read in a text file's
    > contents and keep the carriage returns and line feeds?
    >
    > Thanks,
    > Dave
    >
    > #include <algorithm>
    > #include <fstream>
    > #include <iostream>
    > #include <iterator>
    > #include <string>
    >
    > using namespace std;
    >
    > //
    > ****************************************************************************
    > *
    > //
    > ****************************************************************************
    > *
    > //
    > ****************************************************************************
    > *
    > int main()
    > {
    > string file_contents;
    > ifstream file_stream("test_1.txt");
    >
    > copy(
    > istream_iterator<char>(file_stream),
    > istream_iterator<char>(),
    > back_inserter(file_contents)
    > );
    >
    > cout << file_contents << endl;
    > } // main
    >
    >
    >


    Use the following to deactivate skipping whitespace:

    file_stream.setf(0, ios::skipws) ;


    I wouldn't expect that one would need to do this. But it seems it is
    necessary in my implementation as well. Anyone have any comments about
    what the standard says about this?

    Alan
     
    Alan Johnson, May 20, 2004
    #2
    1. Advertising

  3. Dave

    David Harmon Guest

    David Harmon, May 21, 2004
    #3
  4. Dave

    Jerry Coffin Guest

    "Dave" <> wrote in message news:<>...
    > Hello all,
    >
    > The scheme shown below to move a text file's contents into a std::string
    > works with one exception: it drops the carriage return and line feed
    > characters. How may I, in a Standard-compliant way, read in a text file's
    > contents and keep the carriage returns and line feeds?


    int main()
    {
    std::string file_contents;
    std::ifstream file_stream("test_1.txt");

    std::eek:stringstream temp;
    temp << file_stream.rdbuf();

    file_contents = temp.str();
    std::cout << file_contents << std::endl;
    return 0;
    }

    This also has the bonus that it's usually quite a bit faster.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, May 21, 2004
    #4
  5. Dave

    Siemel Naran Guest

    "Dave" <> wrote in message

    > The scheme shown below to move a text file's contents into a std::string
    > works with one exception: it drops the carriage return and line feed
    > characters. How may I, in a Standard-compliant way, read in a text file's
    > contents and keep the carriage returns and line feeds?


    > string file_contents;
    > ifstream file_stream("test_1.txt");
    >
    > copy(
    > istream_iterator<char>(file_stream),
    > istream_iterator<char>(),
    > back_inserter(file_contents)
    > );


    Does opening the file in binary mode fix it?

    ifstream file_stream("test_1.txt", ios::binary);
     
    Siemel Naran, May 21, 2004
    #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. Jeremy Chapman

    Load contents of a text file into a text box

    Jeremy Chapman, Aug 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    765
    Tommy
    Aug 15, 2003
  2. Peter Jansson
    Replies:
    5
    Views:
    6,324
    Ivan Vecerina
    Mar 17, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    448
  4. Jeffrey Walton
    Replies:
    10
    Views:
    944
    Mathias Gaunard
    Nov 26, 2006
  5. Sergey Lukoshkin
    Replies:
    7
    Views:
    447
    Daniel Pitts
    Jul 9, 2010
Loading...

Share This Page