Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dataIO_Text_impl.h
Go to the documentation of this file.
1 
14 #ifndef DATAIO_TEXT_IMPL_INCLUDED
15 #define DATAIO_TEXT_IMPL_INCLUDED
16 
17 template<typename T>
18 void DataIO_Text::read_file_base(T *v, const size_t n, const string& filename)
19 {
21  vout.detailed(m_vl, "reading data to %s.\n", filename.c_str());
22 
23  std::fstream fs(filename.c_str(), std::ios::in);
24  if (!fs.is_open()) {
25  vout.crucial(m_vl, "Error at dataIO_Text_impl: read file failed.\n");
26  exit(EXIT_FAILURE);
27  }
28 
29  for (size_t i = 0; i < n; ++i) {
30  fetch_data<T>(v[i], fs);
31  }
32 
33  fs.close();
34  }
35 
36  vout.general(m_vl, "read successful.\n");
37 }
38 
39 
40 template<typename T>
41 void DataIO_Text::write_file_base(const T *v, const size_t n, const string& filename, const bool append)
42 {
44  vout.detailed(m_vl, "writing data to %s.\n", filename.c_str());
45 
46  std::fstream fs(filename.c_str(), std::ios::out | (append ? std::ios::app : std::ios::trunc));
47  if (!fs.is_open()) {
48  vout.crucial(m_vl, "Error at dataIO_Text_impl: write file failed.\n");
49  exit(EXIT_FAILURE);
50  }
51 
52  fs.setf(std::ios_base::scientific, std::ios_base::floatfield);
53  fs.precision(m_format_precision);
54 
55  for (size_t i = 0; i < n; ++i) {
56  store_data<T>(v[i], fs);
57  }
58 
59  fs.close();
60  }
61 
62  vout.general(m_vl, "write successful.\n");
63 }
64 
65 
66 template<>
67 inline void
68 DataIO_Text::store_data<double> (const double& v, fstream& fs)
69 {
70  fs << v << std::endl;
71 }
72 
73 
74 template<>
75 inline void
76 DataIO_Text::store_data<dcomplex> (const dcomplex& v, fstream& fs)
77 {
78  fs << real(v) << "\t" << imag(v) << std::endl;
79 }
80 
81 
82 template<>
83 inline void
84 DataIO_Text::fetch_data<double> (double& v, fstream& fs)
85 {
86  double val;
87 
88  fs >> val;
89  v = val;
90 }
91 
92 
93 template<>
94 inline void
95 DataIO_Text::fetch_data<dcomplex> (dcomplex& v, fstream& fs)
96 {
97  double val_r, val_i;
98 
99  fs >> val_r >> val_i;
100  v = cmplx(val_r, val_i);
101 }
102 
103 
104 #endif /* DATAIO_TEXT_IMPL_INCLUDED */
void read_file_base(T *v, const size_t n, const string &)
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
void general(const char *format,...)
Definition: bridgeIO.cpp:197
int m_format_precision
Definition: dataIO_Text.h:83
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
Bridge::VerboseLevel m_vl
Definition: dataIO.h:64
void write_file_base(const T *v, const size_t n, const string &, const bool append=true)
static bool is_primary()
check if the present node is primary in small communicator.