-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.cpp
More file actions
110 lines (87 loc) · 2.94 KB
/
notepad.cpp
File metadata and controls
110 lines (87 loc) · 2.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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string filename, line, content;
char option;
cout << "Welcome to Notepad" << endl;
cout << "Please select an option:" << endl;
cout << "R: Read a file" << endl;
cout << "W: Write to a file" << endl;
cout << "E: Edit a file" << endl;
cin >> option;
switch (option) {
case 'R':
case 'r':
{
cout << "Enter the name of the file to read: ";
cin >> filename;
// Open the file for reading
ifstream inputFile(filename.c_str());
if (inputFile.is_open()) {
// Read the contents of the file
while (getline(inputFile, line)) {
content += line + "\n";
}
// Display the contents of the file
cout << "Contents of " << filename << ":" << endl;
cout << content << endl;
} else {
// File not found
cout << "File not found" << endl;
}
// Close the file
inputFile.close();
break;
}
case 'W':
case 'w':
{
cout << "Enter the name of the file to write to: ";
cin >> filename;
// Open the file for writing
ofstream outputFile(filename.c_str());
// Get the content to write to the file
cout << "Enter the content to write to " << filename << ":" << endl;
cin.ignore(); // ignore the newline character from previous input
getline(cin, content);
// Write the content to the file
outputFile << content;
// Close the file
outputFile.close();
cout << "File saved" << endl;
break;
}
case 'E':
case 'e':
{
cout << "Enter the name of the file to edit: ";
cin >> filename;
// Open the file for reading and writing
fstream editFile(filename.c_str(), ios::in | ios::out);
if (editFile.is_open()) {
// Get the content to write to the file
cout << "Enter the new content for " << filename << ":" << endl;
cin.ignore(); // ignore the newline character from previous input
getline(cin, content);
// Overwrite the old content with the new content
editFile.seekg(0, ios::end);
editFile << content;
// Close the file
editFile.close();
cout << "File saved" << endl;
} else {
// File not found
cout << "File not found" << endl;
}
break;
}
default:
cout << "Invalid option" << endl;
break;
}
std::cin.get();
std::cin.get();
return 0;
}