6

This is pretty bare-bones, meant to just get the program going so I can debug the more complex parts:

//open file
cout << "Input File Name: ";
string fileName;    
cin >> fileName;
ifstream file;  
file.open(fileName, ios_base::in);
if (!(file.good())){
    cout << "File Open Error\n";        
    return 0;
}

The program compiles fine. If I execute the debug executable from \Projects\[this project]\Debug\[program].exe by just double-clicking or browsing there via cmd, it will open the file (which is stored in that same directory) and the rest of the program hums along nicely (until it gets to the buggy parts I actually want to debug, anyways).

However, if I try to 'Start Debugging' from within VS2013, the above fails; it prints the error and immediately closes. The cmd window that the program is executing in when in debugging mode of course shows the directory in the title area, and it is definitely the same directory, but I guess it's looking for the file somewhere else. I tried copying it to the volume root as well, no joy there. I am certain this worked just fine in earlier versions of VS, but maybe I'm just brain-farting here. Any ideas?

3
  • The default working directory for startup of a program running under the visual studio debugger is the project directory (where your vcprojx file is located). The title you see is the path of the executable; not the working directory. Either specify a fully qualified path in your input or change the project settings to the working directory you want, then fire up the process in the debugger. Commented Apr 28, 2014 at 6:53
  • When you run/debug the program from Visual Studio, current directory is project directory. Commented Apr 28, 2014 at 6:53
  • 1
    Because it's microsoft. They write software to disrupt developers and gather intelligence on what people are coding. They have no interest whatsoever in enabling developers, if they did they'd lose their monopoly. Visual Studio is designed to be as crippling and as unergonomic a development environment as it can possibly be. There's not even any sane memory leak tools / valgrind or electric fence systems for hunting down memory leaks. Windows development is hell. Commented Jun 27, 2017 at 15:07

1 Answer 1

10

In the project properties in Visual Studio, you can set the current directory in which to start the debugged program. By default, this is set to the location of the .vcxproj file, i.e. it's not the location of the executable.

At the same time, relative paths passed to std file stream constructors are interpreted relative to the program's current directory, which is why it fails for you when debugging but not when running directly. If you instead launched the program using these cmd commands:

>cd some\random\dir
>C:\path\to\your\Projects\[this project]\Debug\[program].exe

It would fail in exactly the same way.

To modify the startup directory used by Visual Studio when debugging, go to Project > Properties > Configuration Properties > Debugging > Working Directory. Note that the setting is configuration-specific (i.e. you can have a different startup dir for each configuration). If you want to set it to the directory containing the executable, you can use the macro $(OutDir).

Perhaps preferably, you might want to move the data file into the project source directory, as it's not a build artifact.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, thanks. I knew it had to be something simple like that, but google let me down. I'm pretty certain I've never needed to do that in prior versions of VS (or maybe I just figured out to give it the file as \Debug\[file], who knows...) Anyways, thanks for getting me over this brain fail.
@jmassey Then you must have been placing the files elsewhere. The default working directory has always been $(ProjectDir), i.e. the location of the .vc[x]proj.
Yeah, I added an edit above; it may just have been that in the past I've figured out how to do it on my own. I've not had to poke VS for half a year or so, and I last used 2010. Maybe all the shiny blinded me hehe. Thanks again, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.