3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
container.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
27#ifndef DUMUX_IO_CONTAINER_HH
28#define DUMUX_IO_CONTAINER_HH
29
30#include <iostream>
31#include <ios>
32#include <iomanip>
33#include <fstream>
34#include <iterator>
35
36#include <dune/common/exceptions.hh>
37
38namespace Dumux {
39
49template<typename Container>
50void writeContainerToFile(const Container& v,
51 const std::string& filename,
52 int floatPrecision = 6)
53{
54 std::ofstream outfile(filename, std::ios::out);
55 outfile << std::scientific << std::setprecision(floatPrecision);
56 std::ostream_iterator<typename Container::value_type> it(outfile, "\n");
57 std::copy(v.begin(),v.end(), it);
58}
59
66template<typename Container>
67Container readStreamToContainer(std::istream& stream)
68{
69 Container v;
70 std::istream_iterator<typename Container::value_type> it(stream);
71 std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v));
72 return v;
73}
74
83template<typename Container>
84Container readFileToContainer(const std::string& filename)
85{
86 std::ifstream infile(filename, std::ios::in);
87 if (!infile)
88 DUNE_THROW(Dune::IOError, "Could not open file: " << filename);
89 return readStreamToContainer<Container>(infile);
90}
91
92} // end namespace Dumux
93
94#endif
void writeContainerToFile(const Container &v, const std::string &filename, int floatPrecision=6)
Writes a container to file.
Definition: container.hh:50
Definition: adapt.hh:29
Container readFileToContainer(const std::string &filename)
Read a simple text file into a container.
Definition: container.hh:84
Container readStreamToContainer(std::istream &stream)
Read an input stream into a container.
Definition: container.hh:67