-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io1.cpp
49 lines (45 loc) · 1.16 KB
/
file_io1.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
#include <string>// for strings
#include <iostream>// for I/O
#include <fstream>// for file I/O
#include <iomanip>// for setw()
#include <cstdlib>// for exit()
using namespace std;
// forward declarations
void writeCharsetToFile (const string& filename);
void outputFile (const string& filename);
int main () {
writeCharsetToFile("charset.out");
outputFile("charset.out");
}
void writeCharsetToFile (const string& filename) {
// open output file
ofstream file(filename);
// file opened?
if (! file) {
// NO, abort program
cerr << "can’t open output file \"" << filename << "\""
<< endl;
exit(EXIT_FAILURE);
}
// write character set
for (int i=32; i<256; ++i) {
file << "value: " << setw(3) << i << " "
<< "char: " << static_cast<char>(i) << endl;
}
}// closes file automatically
void outputFile (const string& filename) {
// open input file
ifstream file(filename);
// file opened?
if (! file) {
// NO, abort program
cerr << "can’t open input file \"" << filename << "\""
<< endl;
exit(EXIT_FAILURE);
}
// copy file contents to cout
char c;
while (file.get(c)) {
cout.put(c);
}
}// closes file automatically