file_io
Loading...
Searching...
No Matches
file_io.cpp
Go to the documentation of this file.
1#include "file_io.h"
2#include <cassert>
3#include <fstream>
4#include <iostream>
5#include <stdexcept>
6#include <string>
7#include <vector>
8using namespace std;
9
10// [Str10, p.364]
11void fill_vector(istream& istr, vector<double>& v)
12{
13 double d=0;
14 while ( istr >> d) {v.push_back(d);} // Einlesen
15 if (!istr.eof())
16 { // Fehlerbehandlung
17 cout << " Error handling \n";
18 if ( istr.bad() ) {throw runtime_error("Schwerer Fehler in istr");}
19 if ( istr.fail() ) // Versuch des Aufraeumens
20 {
21 cout << " Failed in reading all data.\n";
22 istr.clear();
23 }
24 }
25 v.shrink_to_fit(); // C++11
26}
27
28
29void read_vector_from_file(const string& file_name, vector<double>& v)
30{
31 ifstream fin(file_name); // Oeffne das File im ASCII-Modus
32 if( fin.is_open() ) // File gefunden:
33 {
34 v.clear(); // Vektor leeren
35 fill_vector(fin, v);
36 }
37 else // File nicht gefunden:
38 {
39 cout << "\nFile " << file_name << " has not been found.\n\n" ;
40 assert( fin.is_open() && "File not found." ); // exeption handling for the poor programmer
41 }
42}
43
44void write_vector_to_file(const string& file_name, const vector<double>& v)
45{
46 ofstream fout(file_name); // Oeffne das File im ASCII-Modus
47 if( fout.is_open() )
48 {
49 for (unsigned int k=0; k<v.size(); ++k)
50 {
51 fout << v.at(k) << endl;
52 }
53 }
54 else
55 {
56 cout << "\nFile " << file_name << " has not been opened.\n\n" ;
57 assert( fout.is_open() && "File not opened." ); // exeption handling for the poor programmer
58 }
59}
60
void write_vector_to_file(const string &file_name, const vector< double > &v)
Definition file_io.cpp:44
void read_vector_from_file(const string &file_name, vector< double > &v)
Definition file_io.cpp:29
void fill_vector(istream &istr, vector< double > &v)
Definition file_io.cpp:11