I have the function
void foo()
{
std::string path = "Test.txt" ;
std::ifstream file;
file.open(path);
if (file.good()) // if the file opened up
{
std::cout << "YAY" << std::endl;
}
else
{
std::cout << "ERROR!" << std::endl;
getError();
}
}
I have a file "Test.txt" in the folder where I have the .exe file . Running the program direclly through the cmd follows a success in openning the file , but running the program through Visual studio fails to open the file . I tried to open the file with a full path , but the result remained the same .
By default, Visual Studio's working directory is the project's folder (where you can usually see the code files and the project files), so make sure your file is there. The EXE is found at the Debug
folder, which is irrelevant, as it is not Visual Studio's working directory.
You can change the working directory this way:
https://msdn.microsoft.com/en-us/library/ms171340%28v=vs.90%29.aspx
Cheers.