-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGroup_B_02.cpp
More file actions
37 lines (31 loc) · 991 Bytes
/
Copy pathGroup_B_02.cpp
File metadata and controls
37 lines (31 loc) · 991 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
31
32
33
34
35
36
37
/*
Write a C++ program that creates an output file, writes information to it, closes the file
andopen it again as an input file and read the information from the file.
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Create an output file and write information to it
ofstream outputFile("output.txt");
if (!outputFile.is_open()) {
scerr << "Failed to open the output file." << endl;
return 1;
}
string data = "This is a sample text written to the file.";
outputFile << data << endl;
outputFile.close();
// Open the same file as an input file and read the information
ifstream inputFile("output.txt");
if (!inputFile.is_open()) {
cerr << "Failed to open the input file." << endl;
return 1;
}
string readData;
while (getline(inputFile, readData)) {
cout << "Read from the file: " << readData << endl;
}
inputFile.close();
return 0;
}