-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestpad3.cpp
More file actions
122 lines (100 loc) · 3.22 KB
/
testpad3.cpp
File metadata and controls
122 lines (100 loc) · 3.22 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <memory>
#include <limits>
#include <cstdlib>
#include <cerrno>
#include <cstring>
using namespace std;
fstream working;
string filename;
int status;
enum {Op_No, Op_New, Op_Existing};
int startup();
int fileInit(string fn, int stat);
void showFstreamError(const fstream& ofs, const string& filename);
int main() {
int t0;
do {
t0 = startup();
} while (t0 != 0);
if(fileInit(filename, status) != 0) {
cerr << "File initialization failed." << endl;
return 1;
}
return 0;
}
int startup() {
cout << "In a mood of editing? (0 for no, 1 for new, 2 for existing)" << endl;
unique_ptr<char> response = make_unique<char>();
*response = cin.get();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (isdigit(*response) ? *response - '0' : *response) {
case Op_No:
cout << "You chose not to edit." << endl;
cout << "Exiting the program." << endl;
exit(0);
case Op_New:
cout << "You chose to create a new file." << endl;
status = Op_New;
break;
case Op_Existing:
cout << "You chose to edit an existing file." << endl;
status = Op_Existing;
break;
default:
cerr << "Invalid choice." << endl;
return 1;
}
do {
cout << "Enter the name of the text file to work on: ";
getline(cin, filename);
if (filename.empty() && filename.size() < 5 && !(filename.substr(filename.size() - 1 - 3) == ".txt")) {
cerr << "Filename cannot be empty and must be ended in \".txt\" Please try again." << endl;
}
} while (filename.empty() && filename.size() < 5 && !(filename.substr(filename.size() - 1 - 3) == ".txt"));
return 0;
}
int fileInit(string fn, int stat) {
if (stat == Op_New) {
fstream newFile(fn);
if (!newFile.is_open()) {
cerr << "Error creating new file." << endl;
showFstreamError(newFile, fn);
return 1;
}
working.open(fn);
if (!working.is_open()) {
cerr << "Error opening file for writing." << endl;
showFstreamError(working, fn);
return 1;
}
cout << "New file created: " << fn << endl;
} else if (stat == Op_Existing) {
working.open(fn, ios::in | ios::out);
if (!working.is_open()) {
cerr << "Error opening file for reading/writing." << endl;
showFstreamError(working, fn);
return 1;
}
cout << "Existing file opened: " << fn << endl;
}
return 0;
}
void showFstreamError(const fstream& fs, const string& filename) {
cerr << "Error opening or writing to file: " << filename << endl;
if (errno != 0) {
cerr << "System error: " << strerror(errno) << endl;
}
if (fs.fail()) {
cerr << "Stream status: failbit set (operation failed)." << endl;
}
if (fs.bad()) {
cerr << "Stream status: badbit set (serious I/O error)." << endl;
}
if (fs.eof()) {
cerr << "Stream status: eofbit set (unexpected end of file)." << endl;
}
}