Trouble reading a file into program

My program is supposed to prompt the user to enter the name of a file (which for my case is simply data.txt). If it works then it works. But if the file name is incorrect, the program will display an error message warning the user that they have 2 more attempts to enter the correct file name until the program ends. The problem I am having with my program is that I do not know how to actually get the file to read correctly. My guess is that my data.txt file is not in the correct location so it's not being read into the program. I could be wrong though, as this is my first time reading in a file.

Here's what I have so far, Thanks.

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
  	char xfileName[16];
	ifstream xfile;

	cout <<"Enter the file name. (max. 15 characters)";
	cin >> xfileName;

	xfile.open("xfileName");
	if(xfile.fail())
	{
		int i = 2;
		

		while(i > 0)
		{
			cout <<"Failed to open " << xfileName << endl;
			cout <<"Please check for errors and re-enter file name.\n";
			cin >> xfileName;

			xfile.open("xfileName");
			if(xfile.fail())
			{
				i--;
			}
		}
	}
	else
	{
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/files/

Have a look at the sample for opening a text file and use that as a model.
Topic archived. No new replies allowed.