-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.h
71 lines (59 loc) · 1.17 KB
/
file.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef file_h
#define file_h
#include <iostream>
#include <fstream>
using namespace std;
namespace file {
void append_path(string &path, string const &append) {
while (path.length() > 0) {
char c = path.back();
#ifdef _WIN32
if (c != '\\') {
path.pop_back();
} else {
break;
}
#else
if (c != '/') {
path.pop_back();
} else {
break;
}
#endif
}
path += append;
}
string prompt_file_open (string path = "") {
ifstream file;
string contents = "";
do {
string name = "";
cout << "Enter file to open: ";
getline(cin, name);
append_path(path, name += ".xml");
file.open(path, ios::in);
string line = "";
while (getline(file, line)) {
contents += line + '\n';
}
if (contents == "") {
cout << "Unable to open file " << name << endl;
} else {
break;
}
} while (true);
return contents;
}
string file_open (string path, string const &name) {
append_path(path, name);
ifstream file {path};
// file.open(name, ios::in);
string contents = "";
string line = "";
while (getline(file, line)) {
contents +=line + '\n';
}
return contents;
}
} // namespace file
#endif