Asking user for file name and displaying file

I cannot get this code to work and I am wondering why, it's probably something small that I am overlooking but my research hasn't provided to be useful. I need to have the user input a file name, if the name is correct then display the .txt file, if the name is incorrect display error. I can get the code to work if I hardcode the location for txt file. Thanks for the help in advance. My window just stops at the system("PAUSE") statement and doesn't display the file.

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
31
32
33
34
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdexcept>
#include <exception>

using namespace std;

int main()
{
	string fileName;
	string line;

	cout << "Please enter file name <in that same file as your executable>: ";
	cin >> fileName;
	ifstream dataFile(fileName);
	if (dataFile.good())
	{
		while (getline(dataFile, line))
		{
			cout << line << '\n';
		}
		dataFile.close();
	}
	else
	{
		perror("File Failed to Open");
		system("PAUSE");
		return 0;
	}
	system("PAUSE");
	return 0;
}
Last edited on
I cannot get this code to work
Are your program:
1) Not compiles
    * If so, give us error message and corresponding code part.
2) Not running
    * Are you sure that it is not a problem of automatically closing console?
    * How do you run it?
    * Is there any error messages?
3) Gives an error when run
    * Is it system error message or error reported in console?
    * Give us error message and input which leads to error.
4) Not giving correct results
    * Tell what you entered, what you expected, and what you got.
    * Are you sure that results you expected are correct?
I realized before you posted that that I didn't give a reason, the reason is now displayed above. It compiles, runs, doesn't display errors, but doesn't display the contents of the .txt file.
Do you have C++11 enabled in your compiler?

The ifstream constructor accepts a std::string only as of C++11.
If you're not using C++11, then your ifstream constructor should be:
 
  ifstream dataFile(fileName.c_str());


It would be helpful if you explained what happens with the code you posted. Are you getting compile errors? Runtime errors? crashes? or???


I didn't know .c_str() needed to be added, the code just goes to the system("PAUSE") message no matter what is typed at the file name, no compile error, runtime error, or crash
Do you have C++11 enabled in your compiler?
If he was not, compilation would fail.

Are you sure that file is not empty?

If you add std::cout.flush() befor system() call, does it display anything?
I am using the most updated Visual studio 2013, so I believe c++11 is enabled. cout.flush() doesn't stop the system("PAUSE") statement from being displayed, it still shows "Press any key to continue . . ."
So it is not a problem of streams not flushing in time.

I repeat my question: Are you sure that file is not empty? That it is not opened in another program? (I had a problem when I filled input file with data, but forgot to save file, I saw unsaved data in notepad window and wondered why program cannot see it).
Your code is fine and should work (and it works for me).
Okay this is weird, If I run just the .exe it works fine, but if I run my debugger it doesn't display the number from the .txt file. Anyone ever heard of that?
I have an idea, try to manually specify openmode:
ifstream dataFile(fileName, ifstream::in);
YOu do remember that working directory for files run in debuger is project folder?
Last edited on
THATS IT! I thought that it just needed to be in the same folder as the .exe. It is working fine now. Thanks for all the help, look for my next question I need to do some more with this.
Topic archived. No new replies allowed.