1: //test_input_stream.cpp
2: //Illustrates how to test the input stream.
4: #include <iostream>
5: #include <fstream>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program tests a file input stream in certain ways.\n"
11: "It will either open and read a file, reporting the values it "
12: "found.\nOr, it will report an error of some kind.\n";
14: int i1, i2;
16: ifstream inFile;
17: inFile.open("in_data");
18: if (!inFile)
19: {
20: cout << "\nError: Input data file not found ..."
21: "\nCreate data file and run program again.\n";
22: cout << endl; //Why have this statement?
23: return 1;
24: }
26: inFile >> i1 >> i2;
27: if (!inFile)
28: {
29: if (inFile.eof())
30: {
31: cout << "\nError: End of file reached before "
32: "all data read.\n";
33: cout << endl; //Why have this statement?
34: inFile.close();
35: return 2;
36: }
37: else
38: {
39: cout << "\nError: Input data improperly formatted.\n";
40: cout << endl; //Why have this statement?
41: inFile.close();
42: return 3;
43: }
44: }
45: inFile.close();
47: cout << "\nThe file was actually read. The values in it were:\n";
48: cout << " i1: " << i1 << " i2: " << i2 << endl << endl;
49: return 0;
50: }