forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
fileio2.cpp
36 lines (30 loc) · 939 Bytes
/
fileio2.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
// This program shows how C++-based file I/O works.
// It will open a file, read in the first two strings, and print them to the screen.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
// we want to use parameters
int main(int argc, char** argv) {
// verify the correct number of parameters
if (argc != 2) {
cout << "Must supply the input file name as the one and only parameter" << endl;
exit(1);
}
// attempt to open the supplied file
ifstream file(argv[1], ifstream::binary);
// report any problems opening the file and then exit
if (!file.is_open()) {
cout << "Unable to open file '" << argv[1] << "'." << endl;
exit(2);
}
// read in two strings
string s1, s2;
file >> s1;
file >> s2;
// output those strings
cout << s1 << endl;
cout << s2 << endl;
// close the file before exiting
file.close();
}