-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex46.cpp
More file actions
50 lines (37 loc) · 831 Bytes
/
ex46.cpp
File metadata and controls
50 lines (37 loc) · 831 Bytes
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
/*
CPSC 121-0X
Paul De Palma
depalma
Example 46
*/
//Testing for file open errors
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream gfopen(string);
int main()
{
string line, fileName;
ifstream inputFile;
cout << "Enter an input file name" << endl;
getline(cin,fileName); //notice the slightly different usage with a string
inputFile = gfopen(fileName);
getline(inputFile,line); //notice the analogous usage with a file
while (inputFile)
{
cout << line << endl; //'\n' is the default delimiter and not read in.
getline(inputFile,line);
}
inputFile.close();
return 0;
}
ifstream gfopen(string fileName)
{
ifstream fileIn;
fileIn.open(fileName);
if (fileIn)
return fileIn;
cout << "Error opening input file: " << fileName << endl;
exit(0);
}