-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
151 lines (143 loc) · 3.76 KB
/
Copy pathFileSystem.cpp
File metadata and controls
151 lines (143 loc) · 3.76 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "FileSystem.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <cstdio>
#include <exception>
using namespace std;
namespace fs = filesystem;
void FileSystem::createFile(const string &fileName, const string &content)
{
try
{
ofstream newFile(fileName);
// newFile.exceptions(ofstream::failbit | ofstream::badbit);
if (!newFile)
{
cerr << "Cannot create file (Stream error): " << fileName << "\n";
return;
}
newFile << content << "\n";
cout << "Created file: " << fileName << "\n";
}
catch (const exception &e)
{
cerr << "Exception in createFile: " << e.what() << "\n";
}
}
void FileSystem::readFile(const string &fileName)
{
try
{
ifstream in(fileName);
if (!in)
{
cerr << "Cannot read file (File may not exist): " << fileName << "\n";
return;
}
string line;
if (getline(in, line))
{
cout << "File content: " << line << "\n";
}
else
{
cerr << "File is empty or could not read line: " << fileName << "\n";
}
}
catch (const exception &e)
{
cerr << "Exception in readFile: " << e.what() << "\n";
}
}
void FileSystem::deleteFile(const string &fileName)
{
// Using remove from <cstdio> for file deletion
try
{
int result = remove(fileName.c_str());
if (result == 0)
{
cout << "Deleted file: " << fileName << "\n";
}
else
{
perror("Delete failed");
}
}
catch (const exception &e)
{
cerr << "Exception in deleteFile: " << e.what() << "\n";
}
}
void FileSystem::renameFile(const string &oldName, const string &newName)
{
try
{
int result = rename(oldName.c_str(), newName.c_str());
if (result == 0)
{
cout << "Renamed: " << oldName << " -> " << newName << "\n";
}
else
{
perror("Rename failed");
}
}
catch (const exception &e)
{
cerr << "Exception in renameFile: " << e.what() << "\n";
}
}
void FileSystem::listDirectory(const string &path)
{
try
{
cout << "Directory listing for: " << fs::absolute(path) << "\n";
// fs::directory_iterator may throw fs::filesystem_error
for (const auto &entry : fs::directory_iterator(path))
{
cout << " " << entry.path().filename().string() << "\n";
}
}
catch (const fs::filesystem_error &e)
{
cerr << "Filesystem Error in listDirectory: " << e.what() << "\n";
cerr << " Code: " << e.code().message() << "\n";
}
catch (const exception &e)
{
cerr << "General Exception in listDirectory: " << e.what() << "\n";
}
}
void FileSystem::moveFile(const string &src, const string &dst)
{
try
{
// create_directories catch the case where the destination directory does not exist
fs::create_directories(fs::path(dst).parent_path());
// When you call `rename`:
// * The file’s data blocks on disk don’t move.
// * The system only updates the pointer (directory entry → inode) from the old path to the new one.
if (rename(src.c_str(), dst.c_str()) == 0)
{
cout << "Moved: " << src << " -> " << dst << "\n";
}
else
{
perror("Move failed");
}
}
catch (const fs::filesystem_error &e)
{
cerr << "Filesystem Error in moveFile: " << e.what() << "\n";
}
catch (const exception &e)
{
cerr << "Exception in moveFile: " << e.what() << "\n";
}
catch (...)
{
cerr << "Unknown error moving file\n";
}
}