I can not seem to figure out how to add a text file to my project so that the program can find and open a user specified file. I copied this code from my textbook just to try and test where and how to load a text file for opening and reading data from. I have tried adding new item and adding existing item and when i run the program it still cannot find the file even though it is loaded into my project using the above methods.
If you run a program from VS the current directory is the same where the solution file (.sln) is.
If you run the program from the command line the current directory is either in the Debug or Release folder.
Also on line 32 it's better to use perror(nullptr); because it gives a more meaningful error msg.
You need to include <cstdio> for that.
I do not see VS 2017 being much different than VS 2015 that I use. When I started with VS I had a hard time with where to put a text file to use with a program. Eventually I found this path to work well when the IDE is in "Debug" mode. "C:\Users\Owner\Documents\Visual Studio 2015\Projects\Project name\Project Name". Where "Project Name" is the name of the project you are working on.
If you compile the program in "Release" mode the path will change because of where the ".exe" file is put. You can either adjust the path in the program or copy the files to "...\Projects\Project Name\Release" sub directory.
Comment out lines 15 - 18 and add this code and run the program:
Andy I put your code in to test and see if the file was in the right location and found that it needs to go in the same Project folder as my .cpp file then I simply add existing file from that directory to the solution source files.
So I know i have the file in the right location now but the original program still doesnt read and open the file. I dont understand what the issue is as the program is directly from the text so there is no reason it shouldnt work. This is why i didnt think adding the text file to the same location as my .ccp file was working. Am I missing something? Why wont my text file open and display an screen?
Yes I did it says "no such file or directory". I am not sure why Andy's suggestion would be able find the file though. I have the file saved in the the same location as the .cpp file. I tried adding it to the same location as the .sln file and had no luck.
Sorry for the wrong code. I meant to set up the code for output to create a file, but started working with the wrong code.
One possibility, my bit of code put the file name in the variable "iFileName" where as you code takes input from the user. What might be happening there is that the file name entered might be misspelled or the extension is missing.
I tried your code and entered a file name with the extension and it worked.
Also you do not need to add the input or output files to the project solution to use them.
I do not know what you are using as a file name, but on line 16 it would work better to use "std::getline(std::cin, input);" instead of the "std::cin >> input;". So if your file name has a space in it the "std::cin" will read up to the first space and stop leaving the rest of the file name in the input buffer for any next read.