-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandle3.cpp
More file actions
140 lines (120 loc) · 3.46 KB
/
filehandle3.cpp
File metadata and controls
140 lines (120 loc) · 3.46 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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Reads lines from file into vector
vector<string> readLines(string filename) {
ifstream infile(filename.c_str());
vector<string> lines;
string line;
while (getline(infile, line)) {
lines.push_back(line);
}
infile.close();
return lines;
}
// Writes lines from vector to file
void writeLines(string filename, const vector<string>& lines) {
ofstream outfile(filename.c_str());
for (int i = 0; i < lines.size(); i++) {
outfile << lines[i] << endl;
}
outfile.close();
}
// Display lines with line numbers
void displayLines(const vector<string>& lines) {
cout << "\nFile content:\n";
for (int i = 0; i < lines.size(); i++) {
cout << i + 1 << ": " << lines[i] << endl;
}
}
// Backup original file
void backupFile(string filename) {
vector<string> lines = readLines(filename);
string backupName = filename + ".bak";
writeLines(backupName, lines);
cout << "Backup saved as: " << backupName << endl;
}
// Edit multiple lines
void editLines(vector<string>& lines) {
int count;
cout << "How many lines do you want to edit? ";
cin >> count;
for (int i = 0; i < count; i++) {
int lineNumber;
cout << "\nEnter line number to edit (1 to " << lines.size() << "): ";
cin >> lineNumber;
if (lineNumber < 1 || lineNumber > lines.size()) {
cout << "Invalid line number. Skipping.\n";
continue;
}
cin.ignore(); // clear input buffer
string newText;
cout << "Enter new text for line " << lineNumber << ": ";
getline(cin, newText);
lines[lineNumber - 1] = newText;
}
}
// Add new line
void addLine(vector<string>& lines) {
cin.ignore();
string newText;
cout << "Enter text to add as new line: ";
getline(cin, newText);
lines.push_back(newText);
cout << "Line added at the end.\n";
}
// Delete a line
void deleteLine(vector<string>& lines) {
int lineNumber;
cout << "Enter line number to delete (1 to " << lines.size() << "): ";
cin >> lineNumber;
if (lineNumber < 1 || lineNumber > lines.size()) {
cout << "Invalid line number.\n";
return;
}
lines.erase(lines.begin() + (lineNumber - 1));
cout << "Line deleted.\n";
}
int main() {
string filename;
int choice;
cout << "Enter file name: ";
cin >> filename;
vector<string> lines = readLines(filename);
do {
cout << "\n--- Menu ---\n";
cout << "1. Display file content\n";
cout << "2. Edit lines\n";
cout << "3. Add new line\n";
cout << "4. Delete line\n";
cout << "5. Backup file\n";
cout << "6. Save and exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
displayLines(lines);
break;
case 2:
editLines(lines);
break;
case 3:
addLine(lines);
break;
case 4:
deleteLine(lines);
break;
case 5:
backupFile(filename);
break;
case 6:
writeLines(filename, lines);
cout << "Changes saved. Exiting.\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 6);
return 0;
}