version 3.8
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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
15#ifndef DUMUX_IO_CONTAINER_HH
16#define DUMUX_IO_CONTAINER_HH
17
18#include <iostream>
19#include <ios>
20#include <iomanip>
21#include <fstream>
22#include <iterator>
23
24#include <dune/common/exceptions.hh>
25
26namespace Dumux {
27
37template<typename Container>
38void writeContainerToFile(const Container& v,
39 const std::string& filename,
40 int floatPrecision = 6)
41{
42 std::ofstream outfile(filename, std::ios::out);
43 outfile << std::scientific << std::setprecision(floatPrecision);
44 std::ostream_iterator<typename Container::value_type> it(outfile, "\n");
45 std::copy(v.begin(),v.end(), it);
46}
47
54template<typename Container>
55Container readStreamToContainer(std::istream& stream)
56{
57 Container v;
58 std::istream_iterator<typename Container::value_type> it(stream);
59 std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v));
60 return v;
61}
62
71template<typename Container>
72Container readFileToContainer(const std::string& filename)
73{
74 std::ifstream infile(filename, std::ios::in);
75 if (!infile)
76 DUNE_THROW(Dune::IOError, "Could not open file: " << filename);
77 return readStreamToContainer<Container>(infile);
78}
79
80} // end namespace Dumux
81
82#endif
void writeContainerToFile(const Container &v, const std::string &filename, int floatPrecision=6)
Writes a container to file.
Definition: container.hh:38
Definition: adapt.hh:17
Container readFileToContainer(const std::string &filename)
Read a simple text file into a container.
Definition: container.hh:72
Container readStreamToContainer(std::istream &stream)
Read an input stream into a container.
Definition: container.hh:55