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.
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.
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
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?
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.