Repository navigation
Expand file tree
/
Copy pathfileCommands.cpp
More file actions
145 lines (132 loc) · 4.43 KB
/
Copy pathfileCommands.cpp
File metadata and controls
145 lines (132 loc) · 4.43 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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
// Class representing a FileManager for file manipulation operations
class FileManager
{
public:
// Method to remove a file
// Parameters: const string &filename - the name of the file to be removed
// Return type: bool - true if the file is successfully removed, false otherwise
bool removeFile(const string& filename)
{
if (remove(filename.c_str()) == 0)
{
cout << "File '" << filename << "' successfully removed.\n";
return true;
}
else
{
cerr << "Error removing file '" << filename << "'.\n";
return false;
}
}
// Method to display the contents of a file
// Parameters: const string &filename - the name of the file to be displayed
// Return type: void
void catFile(const string filename)
{
ifstream file(filename);
if (file.is_open())
{
file.seekg(0, ios::end);
long long length = file.tellg();
if (length == 0)
{
cout << "The file " << filename << " is empty" << endl;
}
else
{
file.seekg(0, ios::beg);
cout << "Contents of file '" << filename << "':\n";
cout << file.rdbuf() << endl;
}
file.close();
}
else
{
cerr << "Error opening file '" << filename << "'.\n";
}
}
// Method to create a new file
// Parameters: const string &filename - the name of the file to be created
// Return type: void
void touchFile(const string& filename)
{
ofstream file(filename);
if (file.is_open())
{
cout << "File '" << filename << "' created.\n";
file.close();
}
else
{
cerr << "Error creating file '" << filename << "'.\n";
}
}
// Method to move or copy a file
// Parameters: const string &source - the source file, const string &destination - the destination file,
// bool isMove - true if it's a move operation, false if it's a copy operation
// Return type: bool - true if the file is successfully moved or copied, false otherwise
bool moveOrCopyFile(const string& source, const string& destination, bool isMove)
{
ifstream sourceFile(source, ios::binary);
if (sourceFile.is_open())
{
if (sourceFile.peek() == ifstream::traits_type::eof())
{
cerr << "The source file is empty" << endl;
sourceFile.close();
return false;
}
else
{
ofstream destFile(destination, ios::binary);
if (destFile.is_open())
{
destFile << sourceFile.rdbuf();
sourceFile.close();
if (isMove)
removeFile(source);
destFile.close();
}
else
{
cerr << "Error " << (isMove ? "moving" : "copying") << " file from '" << source << "' to '" << destination << "'.\n";
return false;
}
}
return true;
}
else
{
cerr << "Error " << (isMove ? "moving" : "copying") << " file from '" << source << "' to '" << destination << "'.\n";
return false;
}
}
// Method to search for a string in a file and display lines containing the string
// Parameters: const string &filename - the name of the file to be searched, const string &searchString - the string to search for
// Return type: void
void grepFile(const string& filename, const string& searchString)
{
ifstream file(filename);
if (file.is_open())
{
string line;
while (getline(file, line))
{
if (line.find(searchString) != string::npos)
{
cout << line << '\n';
}
}
file.close();
}
else
{
cerr << "Error opening file '" << filename << "'.\n";
}
}
};