-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_twrite_ext_file.cpp
More file actions
43 lines (34 loc) · 916 Bytes
/
read_twrite_ext_file.cpp
File metadata and controls
43 lines (34 loc) · 916 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
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void readTextFile(string filename) {
ifstream file(filename);
if (file.is_open()) {
string line;
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cerr << "Error: Could not open file " << filename << endl;
}
}
void writeTextFile(string filename, vector<string> lines) {
ofstream file(filename);
if (file.is_open()) {
for (string line : lines) {
file << line << endl;
}
file.close();
} else {
cerr << "Error: Could not open file " << filename << endl;
}
}
int main()
{
readTextFile("example.txt");
vector<string> lines = {"This is the first line.", "This is the second line.", "This is the third line."};
writeTextFile("example2.txt", lines);
}