Working with File I/O

I'm working on a program that provides a simple telephone directory.
I have had it working in different stages of its development and believe that I almost have it completely bug free with one exception.
The program reads a list of names and associated phone numbers from an input file. In function main(), I call a function
GetPhoneNumber(ifstream& inFile, string& phoneNumber)
that reads a first name, last name and phone number from a line in the input file and checks to see if they match the user's input. If so, it prints appropriate output. All that works okay. The problem is that when I leave the function, I'm trying to close the input file and then return to main() where the user is invited to look up another name. I have a statement in main that should reopen the input file, and which also tests to see if it opened properly, but I'm getting the error message that the input file cannot be opened. I've done all I know to do. I'm missing something. Here is my code:

01 #include <iostream> // Access cout, cin, endl
02 #include <fstream> // For file I/O
03 #include <string> // Access string type
04
05 using namespace std;
06
07 void GetPhoneNumber(ifstream& inFile, string& phoneNumber);
08
09 int main()
10 {
11 // Input variable
12 ifstream inFile; // Input stream variable
13
14 // Local variables
15 string phoneNumber;
16 char select;
17 bool exit;
18
19 cout << endl << endl << "Welcome to Telephone Directory!"
20 << endl << endl;
21
22 do
23 {
24 inFile.open("telephone1.dat");
25 if (!inFile)
26 {
27 cout << endl << endl << "Can't open input file" << endl;
28 return 1;
29 }
30 GetPhoneNumber(inFile, phoneNumber);
31
32 cout << endl << endl << "Would you like another number? If
33 so, enter " << "'y' for yes." << endl << "Otherwise
34 enter 'x' to exit the " << "program: ";
35 cin >> select;
36 if (select == 'x')
37 exit = true;
38 cout << endl;
39 } while (!exit);
40
41 cout << endl << endl;
42 return 0;
43 }
44
45 void GetPhoneNumber(ifstream& inFile, string& phoneNumber)
46 {
47 string firstName;
48 string lastName;
49 string fileFirstName;
50 string fileLastName;
51 bool nameFound;
52
53 cout << endl << endl << "Enter the first and last name of the person "
54 << "whose number you would like." << endl << "Separate "
55 << "first and last names with a space and be sure to capitalize "
56 << endl << "first and last names: ";
57 cin >> firstName >> lastName;
58
59 inFile >> fileFirstName >> fileLastName >> phoneNumber;
60 nameFound = false;
61
62 while (inFile)
63 {
64 if (firstName == fileFirstName && lastName == fileLastName)
65 {
66 cout << endl << endl << "The phone number for " << fileFirstName
67 << " " << fileLastName << " is (" << phoneNumber.substr(0, 3)
68 << ") " << phoneNumber.substr(3, 3) << "-"
69 << phoneNumber.substr(6, 7) << endl;
70 nameFound = true;
71 inFile >> fileFirstName >> fileLastName >> phoneNumber;
72 }
73 else
74 inFile >> fileFirstName >> fileLastName >> phoneNumber;
75 }
76
77 if (!nameFound)
78 {
79 cout << endl << endl << "The name that you entered " << firstName
80 << " " << lastName << " was not found in the directory!";
81 inFile.close();
82 }
83 else
84 inFile.close();
85 }


It wont let me attached the file that I'm using with dummy phone numbers.

Thanks in advance for any help.
closed account (3hM2Nwbp)
I think that I see your problem here. You're using the following conditional to see if you're done reading the file:

1
2
3
4
5
6
7
while(inFile)
...

Try replacing it with:

while(!inFile.eof())


This may help you because you're reading the file until either the badbit or failbit is set (attempting to read after the EoF flag is set (as operator! doesn't check the state of the EoF flag). That would mean that when you try to reopen the stream, the badbit or failbit is set, causing the both the open method to fail, and your if statement to follow the failed path.

This conclusion was drawn from the reference at:

http://cplusplus.com/reference/iostream/ifstream/

*Edit*
In the future, keep in mind that you can use [ code ] tags while pasting sources.
Last edited on
Thanks Luc, I'm going to try out your suggestion.
Use code tags!
Topic archived. No new replies allowed.