-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
54 lines (44 loc) · 1.22 KB
/
main.cpp
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
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
int main(void) {
std::string opt;
std::fstream *file = new std::fstream();
// open file in append and read mode
file->open("file.txt", std::ios::app | std::ios::in);
if (!file->is_open()) {
std::cerr << "Unable to open file\n";
delete file;
file = nullptr;
return 0x0;
}
do {
std::cout << "Menu\n(W)rite\n(R)ead\n(Q)uit\n> ";
std::getline(std::cin, opt);
if (opt == "w" || opt == "W") {
// clear flags and set put cursor at eof
file->clear();
file->seekp(0, std::ios::end);
std::string data;
std::cout << "Enter string: ";
std::getline(std::cin, data);
*file << data << std::endl;
} else if (opt == "r" || opt == "R") {
// clear flag and set get cursor at begining of file
file->clear();
file->seekg(0, std::ios::beg);
std::string data;
// read until hit eof and print
std::getline(*file, data);
while (!file->eof()) {
std::cout << data << std::endl;
std::getline(*file, data);
}
}
} while (opt != "q" || opt == "Q");
file->close(); // close file handle
delete file;
file = nullptr;
return 0x0;
}