Skip to content
Snippets Groups Projects
Commit f714f582 authored by Timo Koch's avatar Timo Koch
Browse files

[io] Add function to read container from stream

parent 73cbb603
No related branches found
No related tags found
1 merge request!1967Feature/data input
......@@ -55,6 +55,20 @@ void writeContainerToFile(const Container& v,
std::copy(v.begin(),v.end(), it);
}
/*!
* \brief Read an input stream into a container
* \param stream A standard input stream
* \tparam Container The container type, requires begin(), end(), push_back() method
*/
template<typename Container>
Container readStreamToContainer(std::istream& stream)
{
Container v;
std::istream_iterator<typename Container::value_type> it(stream);
std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v));
return v;
}
/*!
* \brief Read a simple text file into a container
* \param filename The filename to write to
......@@ -65,11 +79,8 @@ void writeContainerToFile(const Container& v,
template<typename Container>
Container readFileToContainer(const std::string& filename)
{
Container v;
std::ifstream infile(filename, std::ios::in);
std::istream_iterator<typename Container::value_type> it(infile);
std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v));
return v;
return readStreamToContainer<Container>(infile);
}
} // end namespace Dumux
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment