Reading from a file

Having some trouble with an assignment. Here is the prompt.
Write a program that reads the telephone.dat file and displays its contents on the console. The program should allow the user to add new entries to the file.

I have saved the file into a resource file within visual studio and I am trying to run the code to where the file will be shown when I run the program. I know that the program is running and the file is there because if i misspell the filename it will give me the error message. I don't know how to make the contents of the file display. Here is what I have so far. Any and all tips are welcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inputFile;
	inputFile.open("telephone.dat");
	
	if (inputFile.fail()) {
		cerr << "Error opening file" << endl;
		exit(1);
	}
	
	string item;

	while (!inputFile.eof()) {
		inputFile >> item;
		cout << item;
	}
	return 0;
}
What is happening with lines 19-22? That should be doing what you want I would think.
I thought so as well, my book as it shown that it should've given me the cout I needed. Since it didn't work I found a different code and tried this and I'm still not getting anything whenever I run the code. Here is the new code I tried. I'm at a loss.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string input;
	fstream dataFile;

	dataFile.open("telephone.dat", ios::in | ios::out);

	if (dataFile) 
	{
		getline(dataFile, input);

		while (dataFile)
		{
			cout << input << endl;
			getline(dataFile, input);
		}
		dataFile.close();
	}
	else
	{
		cout << "Error: Cannot open file.\n";
	}
	return 0;
}
Last edited on
I am curious what is being outputted with both methods?

Also, first version was better.

A faster way to start it off though

1
2
3
4
5
6
7
ifstream dataFile("telephone.dat");

if(!dataFile){
    cout << "Error: Cannot open file.\n";
    return -1;
}
// The rest of the code 



Then take a look at the .get(char) method for istreams.
http://www.cplusplus.com/reference/istream/istream/get/
Both methods aren't giving me anything. Although I know that the code is opening the file, because if I change the name of the file to lets says "telephne.dat", the error message will display. Other than that, nothing prints out.
There IS data in the file, correct?

What happened when you tried the method mentioned above?
Yes.
I thought maybe I didn't have the file in the right place, but it is.
Data in the file is
Tony Gaddis 1234567890
Barack Obama 2024561111
James Bond 0070070007
wouldn't I use the getline as opposed to get(char) ?
No, get(char) is what I meant. It will just read the file character by character.
This is so frustrating. I've cross posted on a few help websites and everyone is saying it should be working. I have no idea of where to go from here.
I'll see what happens when I try it on my computer later, and report back. Probably a few hours.
Someone mentioned that the file may not be in the directory of the executable. I don't even understand what that means.
Try putting an absolute path to the file. (I.e. C:\Users\...)
That worked! Holy moly! Thank you!,
but the only thing is that when my professor goes to open the program and execute, I don't think that will work
What is the full path? Pull out any personal information (e.g. username)
I've got it solved. Thanks a ton. You tip helped in a big way.
Topic archived. No new replies allowed.