forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio2.cpp
35 lines (32 loc) · 1.01 KB
/
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
// 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.
// included so we can use cout
#include <iostream>
#include <fstream>
#include <stdlib.h>
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;
// string comparison done with ==, but not shown here
// close the file before exiting
file.close();
}