-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_io.cpp
More file actions
30 lines (27 loc) · 865 Bytes
/
file_io.cpp
File metadata and controls
30 lines (27 loc) · 865 Bytes
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Write to file
ofstream outFile("data.txt"); // Open file for writing
if (outFile) { // Check if file opened successfully
outFile << "Hello, File I/O!\n";
outFile << "Simple example.\n";
outFile.close(); // Close file
} else {
cout << "Cannot open file for writing.\n";
}
// Read from file
ifstream inFile("data.txt"); // Open file for reading
if (inFile) {
string line;
while (getline(inFile, line)) { // Read line by line
cout << line << endl; // Print each line
}
inFile.close(); // Close file
} else {
cout << "Cannot open file for reading.\n";
}
return 0;
}