-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel_3_Task 1_Simple File System Simulation.cpp
More file actions
119 lines (106 loc) · 3.36 KB
/
Level_3_Task 1_Simple File System Simulation.cpp
File metadata and controls
119 lines (106 loc) · 3.36 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
using namespace std;
namespace fs = filesystem;
void sanitize_input (int n);
class SimpleFileSystem {
public:
vector<string> filenames;
SimpleFileSystem () {
loadFilenames ();
}
void loadFilenames () {
filenames.clear ();
for (const auto& entry : fs::directory_iterator (".")) {
if (entry.is_regular_file () && entry.path ().extension () == ".txt") {
filenames.push_back (entry.path ().filename ().string ());
}
}
}
void createFile (const string& filename, const string& content) {
ofstream outFile (filename);
outFile << content;
outFile.close ();
filenames.push_back (filename);
cout << "File '" << filename << "' created.\n";
}
void deleteFile(const string& filename) {
if (fs::remove (filename)) {
cout << "File '" << filename << "' deleted.\n";
// Remove from list
filenames.erase (remove (filenames.begin (), filenames.end (), filename), filenames.end ());
} else {
cout << "File '" << filename << "' not found.\n";
}
}
void readFile (const string& filename) {
ifstream inFile (filename);
if (inFile) {
cout << "Content of '" << filename << "':\n";
string line;
while (getline (inFile, line)) {
cout << line << "\n";
}
} else {
cout << "File '" << filename << "' not found.\n";
}
}
void listFiles () {
cout << "Files:\n";
for (const auto& f : filenames) {
cout << "- " << f << "\n";
}
}
};
int main () {
SimpleFileSystem fs;
int command;
while (true) {
cout << "\n----- Simple File System Simulation in C++ -----\n";
cout << "------------------------------------------------\n";
cout << "\t1. Create\n\t2. Delete\n\t3. Read\n\t4. List\n\t5. Exit\n";
cout << "\nPlease enter commands: ";
cin >> command;
sanitize_input (command); // Input Validation
if (command == 1) {
string filename, content;
cout << "Enter filename: ";
cin >> filename;
cout << "Enter content: ";
cin.ignore (); // ignore leftover newline
getline (cin, content);
fs.createFile (filename, content);
} else if (command == 2) {
string filename;
cout << "Enter filename: ";
cin >> filename;
fs.deleteFile (filename);
} else if (command == 3) {
string filename;
cout << "Enter filename: ";
cin >> filename;
fs.readFile (filename);
} else if (command == 4) {
fs.listFiles ();
} else if (command == 5) {
cout << "\n\nProgram exit successfully.\n";
break;
} else {
cout << "Unknown command.\n";
}
// Reload filenames after delete/create
fs.loadFilenames ();
}
return 0;
}
void sanitize_input (int n) {
if (!cin >> n) {
cout << "Invalid command! Please enter between 1 to 5.\n";
cin.clear (); // Clear error state
cin.ignore (); // Remove bad input
return;
}
}