-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_management_system.cpp
More file actions
119 lines (100 loc) · 2.69 KB
/
Copy pathFile_management_system.cpp
File metadata and controls
119 lines (100 loc) · 2.69 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 <string>
using namespace std;
#define TOTAL_FILES 5
#define TOTAL_BLOCKS 10
// Inode structure
struct Inode {
bool used;
int blockIndex;
int fileSize;
};
// Global structures
Inode inode[TOTAL_FILES];
string dataBlock[TOTAL_BLOCKS];
bool freeBlock[TOTAL_BLOCKS];
// Initialize file system
void initialize() {
for (int i = 0; i < TOTAL_FILES; i++) {
inode[i].used = false;
}
for (int i = 0; i < TOTAL_BLOCKS; i++) {
freeBlock[i] = true;
}
}
// Create file
void createFile(int id) {
if (inode[id].used) {
cout << "File already exists.\n";
return;
}
for (int i = 0; i < TOTAL_BLOCKS; i++) {
if (freeBlock[i]) {
inode[id].used = true;
inode[id].blockIndex = i;
inode[id].fileSize = 0;
freeBlock[i] = false;
cout << "File created successfully.\n";
return;
}
}
cout << "No free block available.\n";
}
// Write file
void writeFile(int id) {
if (!inode[id].used) {
cout << "File does not exist.\n";
return;
}
cout << "Enter data: ";
cin.ignore();
getline(cin, dataBlock[inode[id].blockIndex]);
inode[id].fileSize = dataBlock[inode[id].blockIndex].length();
cout << "Data written successfully.\n";
}
// Read file
void readFile(int id) {
if (!inode[id].used) {
cout << "File does not exist.\n";
return;
}
cout << "File Data: " << dataBlock[inode[id].blockIndex] << endl;
}
// Delete file
void deleteFile(int id) {
if (!inode[id].used) {
cout << "File does not exist.\n";
return;
}
freeBlock[inode[id].blockIndex] = true;
inode[id].used = false;
cout << "File deleted successfully.\n";
}
// Main function
int main() {
int choice, id;
initialize();
do {
cout << "\n--- Simple File System ---\n";
cout << "1. Create File\n";
cout << "2. Write File\n";
cout << "3. Read File\n";
cout << "4. Delete File\n";
cout << "5. Exit\n";
cout << "Enter choice: ";
cin >> choice;
if (choice >= 1 && choice <= 4) {
cout << "Enter File ID (0-4): ";
cin >> id;
}
switch (choice) {
case 1: createFile(id); break;
case 2: writeFile(id); break;
case 3: readFile(id); break;
case 4: deleteFile(id); break;
case 5: cout << "Program terminated.\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}