-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile-operation.cpp
More file actions
125 lines (120 loc) · 4.94 KB
/
file-operation.cpp
File metadata and controls
125 lines (120 loc) · 4.94 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
#include <bits/stdc++.h>
using namespace std;
//... File Name Format: "F:\\C++ Codes\\Operating System Lab\\x.txt"
int main()
{
string operation;
cin >> operation;
cin.ignore();
if (operation == "Create")
{
string name;
getline(cin, name);
ofstream new_file(name.c_str());
cout << "File successfully created";
new_file << "Hello World";
new_file.close();
}
else if (operation == "Rename")
{
string old_name, new_name;
getline(cin, old_name);
getline(cin, new_name);
int mark = rename(old_name.c_str(), new_name.c_str());
if (mark)
{
cout << "Error renaming file!";
}
else cout << "File successfully renamed";
}
else if (operation == "Delete")
{
string file_name;
getline(cin, file_name);
int mark = remove(file_name.c_str());
if (mark)
{
cout << "Couldn't delete file!";
}
else cout << "File successfully deleted";
}
else if (operation == "Copy")
{
string from, to;
getline(cin, from);
getline(cin, to);
ifstream source_file(from.c_str(), ios::binary);
ofstream destination_file(to.c_str(), ios::binary);
string line;
if (source_file and destination_file)
{
while (getline(source_file, line))
{
destination_file << line << "\n";
}
cout << "File successfully copied";
}
else cout << "Couldn't copy file!";
}
else if (operation == "Move")
{
string from, to;
getline(cin, from);
getline(cin, to);
ifstream source_file(from.c_str(), ios::binary);
ofstream destination_file(to.c_str(), ios::binary);
string line;
if (source_file and destination_file)
{
while (getline(source_file, line))
{
destination_file << line << "\n";
}
source_file.close(); //... Otherwise, can't delete
cout << "File successfully moved";
remove(from.c_str());
}
else cout << "Couldn't move file!";
}
else cout << "Invalid operation!";
}
/*//... Sample Input-Output:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
Create
F:\\C++ Codes\\Operating System Lab\\x.txt
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
File successfully created
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
Rename
F:\\C++ Codes\\Operating System Lab\\x.txt
F:\\C++ Codes\\Operating System Lab\\z.txt
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
File successfully renamed
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
Delete
F:\\C++ Codes\\Operating System Lab\\z.txt
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
File successfully deleted
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
Copy
F:\\C++ Codes\\Operating System Lab\\x.txt
F:\\C++ Codes\\x.txt
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
File successfully copied
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
Move
F:\\C++ Codes\\Operating System Lab\\x.txt
F:\\C++ Codes\\x.txt
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
File successfully moved
*///...