Reading Text File

my file is called 'dictionary' and im trying to open it but im not sure what im doing wrong because i keep getting the error message.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main() {
	int length_choice;
	cout << "Starting with 5825 words" << endl;
	cout << "What length string do you want?" << endl;
	
	ifstream infile;
	infile.open("dictionary.txt");
	
	if (!(infile)) {
		cerr << "Unable to open file datafile.txt" << endl;
		   
	}
	cin >> length_choice;
	return 0;
	system("PAUSE");
}
Hello fivestar,

in line 14 try removing the () around "infile".

Hope that helps,

Andy
it didn't change.
Hello fivestar,

Just looking at your code I thought it might be a problem, it was not because it worked for me.

The next thing I would check is the spelling on the file name in the program compared to the file name.

I noticed that you open the file with "dictionary.txt", but in your error message you say "datafile.txt". This is an indication that something is different.

The two basic reasons for the file to not open is the spelling of the name or the file does not exist.

Lines 19 and 20 should be reversed because the pause is never reached because of the return. And it is best to avoid using "system" anything. There are many posts regarding the use of "system". Also you can read through this for suggestions to pause the program. http://www.cplusplus.com/forum/beginner/1988/

Line 18 should come after line 9. I prefer to open the file before I get into anything else.

Hope that helps,

Andy
Make sure that the file "dictionary.txt" is in the current working directory.

Alternatively, use the full path to the file; for example:
"C:/Users/fivestar/documents/dictionary.txt" or "/usr/share/dict/words"
Topic archived. No new replies allowed.