-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternship 1.cpp
More file actions
75 lines (58 loc) · 1.83 KB
/
Copy pathInternship 1.cpp
File metadata and controls
75 lines (58 loc) · 1.83 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
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int choice;
string text;
do {
cout << "\n===== FILE HANDLING TOOL =====\n";
cout << "1. Write to File\n";
cout << "2. Read File\n";
cout << "3. Append to File\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: {
ofstream file("data.txt"); // write mode (overwrite)
cin.ignore();
cout << "Enter text: ";
getline(cin, text);
file << text;
file.close();
cout << "✅ Data written successfully!\n";
break;
}
case 2: {
ifstream file("data.txt");
string line;
if (!file) {
cout << "❌ File not found!\n";
break;
}
cout << "\n📂 File Content:\n";
while (getline(file, line)) {
cout << line << endl;
}
file.close();
break;
}
case 3: {
ofstream file("data.txt", ios::app); // append mode
cin.ignore();
cout << "Enter text to append: ";
getline(cin, text);
file << "\n" << text;
file.close();
cout << "✅ Data appended successfully!\n";
break;
}
case 4:
cout << "👋 Exiting program...\n";
break;
default:
cout << "⚠ Invalid choice!\n";
}
} while(choice != 4);
return 0;
}