Debugging C++ program that reads external files in visual studio code - c++

I have a C++ program that reads some text files from a subdirectory located in the source code folder. The problem is that, the path that I've written in the program is relative and vs code outputs the debug program in /tmp/ directory, which is then unable to read those files. Is there some way of working around it except for giving the absolute path of text files to the program?

You need to configure the working directory for Debugging in the Configuration Properties, So that you can access the file required using relative path.
In project properties window Configuration Properties -> Debugging -> Working Directory update this to the desired path.

Related

Visual Studio looking for a file in the Desktop directory instead of the Project Directory [duplicate]

I've got an a project (native C++ compiled to .exe) that runs fine outside of VS 2010, but inside of VS (with or without the debugger) it gets stuck trying to find a text file located in the same bin folder as it.
Any ideas as to why this would happen? My hunch is that VS messes with where the code looks when trying to open a file, but I don't know enough details to correct this.
Some details:
My .exe calls a function from a .dll I wrote earlier, which in turn tries to find a text file specifying that function's parameters. For convenience I've placed all of these files in the same folder, so finding the text file wouldn't be a problem.
Before opening the file, the function checks that it exists using:
PersistentAssert(Utility::FileExists(Filename), "Parameter file not found");
In VS 2010 this line causes "Parameter file not found" to display, but outside of VS the program manages to find the file.
When launching an executable from visual studio by default it uses the project file directory as the current working directory. You can change it in Configuration Properties -> Debugging -> Working Directory.
However I don't think it's correct behavior to search your programs datafiles etc from the current working directory. Instead your program should find out the directory of the executable and find the files in that directory.
Processes have the concept of a "current directory", which may or may not be the same directory as where the .exe file is located. It sounds like when you run your program inside VS, the current directory is something other than where your .exe is.
Somewhere in the project settings, there should be a place where you can choose what directory will be the current directory when your program starts inside the debugger. Set that to the same location as your .exe and you should be good.

visual studio 2013 modify path variable when running .exe

I have compiled an exe file in Visual Studio 2013, and it depends on some external files to function. I want to keep those files in the same folder that the exe is in. When I run the .exe by clicking on it in file explorer, it loads the files fine. However, when I run it from Visual Studio, it is unable to run because the PATH variable does not include the directory with the necessary files. How can I configure my project to run the program with a modified path variable so that it can access the files properly?
As far as I understand you question correctly. I suspect that this is an issue related to the different directories when you execute. In Visual Studio (to my knowledge), you have a folder named Debug and a second folder named Release.
You may choose in Visual Studio to run the program in either debug or release mode. But you might not have the required files neccessary in both directories.
For example:
I've made a program that reads "Hello World!" from hello.txt, and displays this in the dialog window as a string.
If I store the file in the Release directory, the executable will run fine outside the IDE, when just launching the executable file.
However, if you run the application in debug mode through the IDE (Visual Studio), the program will not find the neccessary file. The program is looking for the file in it's current directory (Debug).
A quick fix to this is to copy the required files to the current working directory. Eventually have a duplicate set of files in both directories at all times.
Hope that I did understand you correct, and that my answer helps you. :)

Project cannot load image assets while running from visual studio IDE

I am porting an old visual studio C++ project. I have a data folder in the project directory that contains all the image assets. The project is build using an external make file.
My problem is, the project cannot load the asset files while running the executable from the VS IDE during debugging. However, if I execute the exe from bin folder, it can load the assets.I am assuming there are some kind of environment issue in the project, but could not figured it out.
Any clue?
My guess is that the program tries to access the assets using a relative path like ..\data\some.data. It works when you run it manually from the bin folder, because the current directory is set to the bin folder itself (the folder from where an application was launched) by default.
But when running projects from the Visual Studio IDE, parameters set under "Project properties > Debugging" tab apply instead. There is an option "Working directory", which defaults to $(ProjectDir), i.e. the folder where the project file resides.
Try changing that to the bin folder, or, if the resulting .exe file is placed directly there, easily just to the $(OutDir) variable.

C++ File - MS Visual Studio

I am making a C++ program that writes data to a file.
I am using MS Visual Studio Express 2012 for Windows Desktop,and my sources and .exe file are in different folders.
So,I need to create two files - one in sources folder and one in .exe-s folder.
But then,if I run my program using compiler,it will write data to the file in sources folder,and if I run .exe file,it will write to it's folder.
But I want all the data to be stored in the same file.
Sorry for bad explanation...
Please help!
Thanks.
You can change working directory in Project properties -> Debugging -> Working directory. Set it to $(TargetDir) (it's a macro for Output Directory which can be set up at General).
This way, when you starting program in VS, all relative paths will begin in binary's directory (instead of project directory, as it was with $(ProjectDir)).

Where do data files go so the Microsoft Visual C++ 2008 debugger can find them?

I am writing code that opens an istream object on a file specified by the user. I want to be able to run the program in the debugger and just type the filename (eg data.txt) at the prompt, not the whole path. I haven't worked out how to do this inside the IDE so I have been saving my .txt file to the debug folder and running the .exe file, but that means I can't step through the program. How do I make it work inside the IDE instead? Thanks.
you can set the working path of the executable (project properties->Debugging->Working Directory), which leads the debugger to start the executable with that path as working directory. This has the advantage that if you set the same path for all your configurations (Debug/Release/...), you only need 1 data.txt on your entire system, which is especially nice if you want to change data.txt or it's name.
I am not sure I understand exactly the problem - is it that your data file is part of the project, but is not in the executable folder when you access it, or is it that the datafile is at another location? If the former, and the data file is part of the project, right-click on the file, and set the Build Action property to "Content". That way, it will get copied to the bin/debug folder where the executable runs when you debug.