Imagine++
IO.h
1// ===========================================================================
2// Imagine++ Libraries
3// Copyright (C) Imagine
4// For detailed information: http://imagine.enpc.fr/software
5// ===========================================================================
6
7namespace Imagine {
10
13 inline void waitKey(const char* message="Press <enter> to continue...") {
14 std::cout << message << std::flush;
15 std::getchar();
16 }
17
27 template <typename T>
28 bool loadText(T& obj, std::string filename) {
29 std::ifstream in(filename.c_str(), std::ios::in);
30 if (!in.is_open()) {
31 std::cerr << "Error: failed to open file '" << filename << "' for reading" << std::endl;
32 return false;
33 }
34 in >> obj;
35 return (!in.fail());
36 }
46 template <typename T>
47 bool loadBinary(T& obj, std::string filename) {
48 std::ifstream in(filename.c_str(), std::ios::in|std::ios::binary);
49 if (!in.is_open()) {
50 std::cerr << "Error: failed to open file '" << filename << "' for reading" << std::endl;
51 return false;
52 }
53 read(in,obj);
54 return (!in.fail());
55 }
66 template <typename T>
67 bool saveText(const T& obj, std::string filename, int precision=-1) {
68 std::ofstream out(filename.c_str(), std::ios::out);
69 if (!out.is_open()) {
70 std::cerr << "Error: failed to open file '" << filename << "' for writing" << std::endl;
71 return false;
72 }
73 if (precision>=0)
74 out.precision(precision);
75 out << obj;
76 return (!out.fail());
77 }
87 template <typename T>
88 bool saveBinary(const T& obj, std::string filename) {
89 std::ofstream out(filename.c_str(), std::ios::out|std::ios::binary);
90 if (!out.is_open()) {
91 std::cerr << "Error: failed to open file '" << filename << "' for writing" << std::endl;
92 return false;
93 }
94 write(out,obj);
95 return (!out.fail());
96 }
97
99}
bool saveText(const T &obj, std::string filename, int precision=-1)
Object saving.
Definition: IO.h:67
bool loadBinary(T &obj, std::string filename)
Object loading.
Definition: IO.h:47
void waitKey(const char *message="Press <enter> to continue...")
Pause in program execution until key press.
Definition: IO.h:13
bool saveBinary(const T &obj, std::string filename)
Object saving.
Definition: IO.h:88
bool loadText(T &obj, std::string filename)
Object loading.
Definition: IO.h:28
Imagine++ namespace.