forked from Chrinkus/stroustrup-ppp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex08_file_concat.cpp
51 lines (44 loc) · 1.28 KB
/
ex08_file_concat.cpp
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
46
47
48
49
50
51
#include "../text_lib/std_lib_facilities.h"
void fill_from_file(ifstream& ifs, vector<string>& v)
// fill vector with strings from file stream
{
ifs.exceptions(ifs.exceptions()|ios_base::badbit);
for (string s; ifs >> s; )
v.push_back(s);
if (ifs.eof()) return;
error("encountered fail during input file stream");
}
int main()
try {
cout << "Please enter first file to read from:\n";
string file_1;
cin >> file_1;
ifstream ifs1 {file_1};
if (!ifs1) error("could not read from file ", file_1);
cout << "Now enter the second file to read from:\n";
string file_2;
cin >> file_2;
ifstream ifs2 {file_2};
if (!ifs2) error("could not read from file ", file_2);
cout << "Finally, enter the destination file:\n";
string file_3;
cin >> file_3;
ofstream ofs {file_3};
if (!ofs) error("could not write to file ", file_3);
vector<string> contents_1;
vector<string> contents_2;
fill_from_file(ifs1, contents_1);
fill_from_file(ifs2, contents_2);
for (string s : contents_1)
ofs << s << ' ';
for (string s : contents_2)
ofs << s << ' ';
}
catch(exception& e) {
cerr << "Error: " << e.what() << '\n';
return 1;
}
catch(...) {
cerr << "Something weird happened..\n";
return 2;
}