Reading from a file

Oct 18, 2014 at 1:08am
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;
}
Oct 18, 2014 at 2:11am
What is happening with lines 19-22? That should be doing what you want I would think.
Oct 18, 2014 at 2:19am
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 Oct 18, 2014 at 2:19am
Oct 18, 2014 at 2:26am
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/
Oct 18, 2014 at 2:31am
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.
Oct 18, 2014 at 3:20am
There IS data in the file, correct?

What happened when you tried the method mentioned above?
Oct 18, 2014 at 3:48am
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
Oct 18, 2014 at 4:13am
wouldn't I use the getline as opposed to get(char) ?
Oct 18, 2014 at 4:35am
No, get(char) is what I meant. It will just read the file character by character.
Oct 18, 2014 at 5:32pm
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.
Oct 18, 2014 at 6:41pm
I'll see what happens when I try it on my computer later, and report back. Probably a few hours.
Oct 18, 2014 at 7:07pm
Someone mentioned that the file may not be in the directory of the executable. I don't even understand what that means.
Oct 18, 2014 at 7:16pm
Try putting an absolute path to the file. (I.e. C:\Users\...)
Oct 18, 2014 at 7:55pm
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
Oct 18, 2014 at 9:45pm
What is the full path? Pull out any personal information (e.g. username)
Oct 18, 2014 at 10:26pm
I've got it solved. Thanks a ton. You tip helped in a big way.
Topic archived. No new replies allowed.