-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
60 lines (50 loc) · 1.57 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
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <cctype>
bool exists(char *filename);
void make_file(char *filename, int exist_state);
void file_creator(int argc, char *const *argv);
constexpr int EXIST_STATE(1);
int main(int argc, char *argv[]) {
file_creator(argc, argv);
if (argc > 1) {
std::cout << "Thanks for using the touch command :)\n";
}
return 0;
}
void file_creator(int argc, char *const *argv) {
for (int i = 1; i < argc; ++i) {
if (exists(argv[i])) {
std::cout << argv[i] << " exists!!\nDo u wanna replace it(y/N)\n";
char ch;
std::cin >> ch;
top :
if (toupper(ch) == 'Y') {
make_file(argv[i], 1);
} else if (tolower(ch) == 'n') {
continue;
} else {
std::cout << "Invalid character\nRe-enter again!\n";
std::cin >> ch;
goto top;
}
} else {
make_file(argv[i], 0);
}
}
}
void make_file(char *filename, int exist_state) {
std::fstream file;
const bool existence = exist_state == EXIST_STATE;
try {
file.open(filename, std::ios::out);
std::cout << (existence ? "File was replaced successfully!!!\n" : "File was created successfully!!!\n");
} catch (std::exception &e) {
std::cerr << (existence ? "Error in file replacement!!!\n"
: "Error in file creation!!!\n");
}
}
bool exists(char *filename) {
struct stat buffer{};
return stat(filename, &buffer) == 0;
}