Repository navigation
Expand file tree
/
Copy pathexpno 12.cpp
More file actions
109 lines (100 loc) · 2.41 KB
/
Copy pathexpno 12.cpp
File metadata and controls
109 lines (100 loc) · 2.41 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
#include <iostream>
#include <fstream>
#include <cstring>
#include <memory>
using namespace std;
// Custom Allocator
template <typename T>
class CustomAllocator
{
public:
using value_type = T;
CustomAllocator() {}
template <typename U>
CustomAllocator(const CustomAllocator<U> &) {}
T *allocate(size_t n)
{
cout << "Allocating memory for " << n << " object(s)\n";
return static_cast<T *>(::operator new(n * sizeof(T)));
}
void deallocate(T *p, size_t n)
{
cout << "Deallocating memory\n";
::operator delete(p);
}
};
// File Record
struct FileRecord
{
char fileName[50];
int fileSize;
};
// Add file record
void addFile()
{
FileRecord file;
cout << "Enter File Name: ";
cin >> file.fileName;
cout << "Enter File Size (KB): ";
cin >> file.fileSize;
ofstream outFile("filesystem.dat", ios::binary | ios::app);
if (outFile)
{
outFile.write(reinterpret_cast<char *>(&file),
sizeof(FileRecord));
outFile.close();
cout << "File record added successfully.\n";
}
else
{
cout << "Unable to open file.\n";
}
}
// Display file records
void displayFiles()
{
ifstream inFile("filesystem.dat", ios::binary);
if (!inFile)
{
cout << "No file records found.\n";
return;
}
FileRecord file;
cout << "\n--- File System Records ---\n";
while (inFile.read(reinterpret_cast<char *>(&file),
sizeof(FileRecord)))
{
cout << "File Name : " << file.fileName << endl;
cout << "File Size : " << file.fileSize << " KB\n";
cout << "--------------------------\n";
}
inFile.close();
}
int main()
{
int choice;
do
{
cout << "\nMINI FILE SYSTEM SIMULATOR\n";
cout << "1. Add File\n";
cout << "2. Display Files\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
addFile();
break;
case 2:
displayFiles();
break;
case 3:
cout << "Exiting File System Simulator...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 3);
return 0;
}