C++ - Is there an elegant way to read in text files?

c++, c++11

Solution

Since you insist on using `std::istream_iterator`, this is one way

#include <sstream>
//....

std::vector<double> v ;
std::string str ;

while ( std::getline(FILE, str) )
{

    std::stringstream ss(str);

    std::copy( std::istream_iterator<double>(ss),
               std::istream_iterator<double>(),
               std::back_inserter(v)
             ) ;

    data.push_back( v ); // data is your vector of vector
    v.clear( );
}

Problem

I'm looking for an elegant way to read in a text file into a 2x2 vector. I'm using this approach to write the data to a text file: ``` ofstream FILE(theFile, ios::out | ofstream::binary); for(const auto& vt : data) { std::copy(vt.begin(), vt.end(), std::ostream_iterator<double>(FILE, " ")); FILE << "\n\n"; } ``` My question is, is there a way to use the `std::istream_iterator` in a similar way to be able to read in the contents of the text file? EDIT: Data: ``` std::vector<std::vector<double> > data; ```

Original source