-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile Creating, Writing, Reading.cpp
More file actions
45 lines (34 loc) · 1.13 KB
/
Copy pathFile Creating, Writing, Reading.cpp
File metadata and controls
45 lines (34 loc) · 1.13 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
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream file ("abc.txt"); // Creates and open the file
// Writes to the file
file << "This is Ajmal Hussain, a C++ enthusiast.\n";
file << "Set up push notifications or calendar events to stay on track for your learning goals.\n";
file << "Hello everyone! I'm Ajmal, aspiring MERN stack from Rai University Ahmedabad.\n";
file << "Files can be tricky, but it is fun enough!\n";
// Read from the text file
ifstream read ("abc.txt");
if (!read) {
cout << "File not found.\n";
return 0;
}
// Close the file
file.close();
cout << "Content of abc.txt:\n";
// Create a string, which is used to output the text file
string line;
// Use a while loop together with the getline() function to read the file line by line
while (getline (read, line)) {
// Output the text from the file
cout << line << endl;
}
// Close the file
file.close();
return 0;
}
/*
Always close after writing.
Always close before reading.
*/