-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTxt.cpp
More file actions
42 lines (31 loc) · 1.6 KB
/
Copy pathFileTxt.cpp
File metadata and controls
42 lines (31 loc) · 1.6 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
#include<iostream>
#include <fstream>
#include <string> `
using namespace std;
int main() {
const string filename = "sample_output.txt";
ofstream outFile(filename);
if (!outFile.is_open()) {
cerr << "Error: Could not create or open the output file." << endl;
return 1;
}
cout << "Writing data to " << filename << "...\n";
outFile << "Line 1: Hello, C++ File I/O!\n";
outFile << "Line 2: Demonstration of writing to a file.\n";
outFile << "Line 3: Storing integer value = " << 100 << "\n";
outFile << "Line 4: NMIET\n";
outFile.close();
cout << "File closed successfully after writing.\n\n";
ifstream inFile(filename);
if (!inFile.is_open()) {
cerr << "Error: Could not open the file for reading." << endl;
return 1;
}
cout << "--- Contents of " << filename << " ---" << endl;
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}