Pass file as an argument to visual studio - c++

I try to debug a program in VS2010. In Windows PowerShell I type./Nbody.exe ./config.txt. But, when I enter .\config.txt or other forms of it in the Command Argument form Debugging section from Property Manager window it seems that my program cannot find the file. In fact this part of my code respond:
ifstream CSVfile(CSVFileName);
if (CSVfile == NULL) {
cout << "ERROR: Cannot Open CSV file" << endl;
exit(1);
}

Visual studio calcutates relative paths starting at working dir path. So, you have to use visual studio variables as $(ProjectDir) to compound the paths of your files.
Something like: $(ProjectDir)\config.txt, otherwise you have to set the path relative to working dir path.
Let's say your structure is this:
project_name
+ project_name
| project_name.sln
| debug
| project_name.exe
| config.txt
+ ipch
and you have .\project_name\project_name as working directory.
The path you enter in the project configuration for debugging has to be relative to the working dir:
..\debug\config.txt
Update
As #Goku states in his (I'm assuming Goku is a 'he', :) ) comment:
For VS2017, $(ProjectDir) already contained a '\' concatenated on the directory name.

Related

Path to input files in VS 2019

Visual Studio does not seem to be finding input files when I try to compile under "Debug":
/* in.dat exists in
\ProjectName\
\ProjectName\Debug\
\Debug\
*/
string name = "in.dat";
ifstream ifs(name); // nullptr...
Are there specific places VS looks for input files? Specifying an absolute pathname did not solve the problem.
There is a setting in the current project settings for working directory.
When you build a project in visual studio, the executable is run in this directory, regardless of which directory the executable is in.
The working directory is found under Project Settings >> Configuration Properties >> Debugging
More information about properties pages:
https://learn.microsoft.com/en-us/cpp/build/working-with-project-properties?view=vs-2019

Opening a folder in Visual Studio from command prompt

I see demos where a folder with an angular app is opened in visual studio code using
code .
from the terminal.
Could I do the same with Visual Studio. Perhaps I should write Powershell Scripts to accomplish the same?
I was able to open a folder from command line using the following in VS 2017
devenv.exe /Edit <path to folder>
Locate the .exe file of the IDE environment (should be found in the IDE folder in the directory on which VS 2017 was installed). On my system, the exact path for the .exe file is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE
Yours might differ, but you should be able to find it. If not, just use the windows search feature to look for devenv.exe.
Once you've located the directory on which devenv.exe resides, proceed to add the path of that directory to your PATH environment variable. There are 3 ways to do that. The easiest way is to use the Windows GUI-based environment editor. Or you could use a third party environment editor (I recommend RapidEE). Another way is to do it directly from the command prompt.
And that's basically it. You're good to go. So let's say you have a directory containing all your source codes and header files at C:\DevFiles. Now, you can just launch your command prompt and from there either cd to C:\DevFiles and then type devenv . OR you can straight away type devenv C:\DevFiles
I think this is currently not possible.
The first requirement is to be able to start Visual Studio from the command line.
The equivalent of the “code” command is “devenv”. If you type this in your command line Visual Studio opens. Just ensure that the command is included in your path if you use a normal command prompt. It is already included in the path variable if you use the Visual Studio Command prompt.
The dot in the code . sets the current directory as working directory. If you are running < VS 2017 you are already out of luck now. In older version of Visual Studio it is not possible to work with such a folder approach. However, in Visual Studio 2017, you can open code from nearly any type of directory-based project into Visual Studio without the need for a solution or project file.
This could all be so easy now if only it would possible to pass this information to the devenv command.
However this is not the case: https://learn.microsoft.com/en-us/visualstudio/ide/reference/devenv-command-line-switches
Thus you would have to find another way to make Visual Studio 2017 in effect do something like File > Open > Folder and set the current directory as working folder.
This might be possible if you write a Visual Studio extension and hook up on the connected event.
But in essence the answer to your question is “not possible at the moment.”
The OP has been answered already and my answer only expounds on what has been said. The BLUF is <pathToVS>devenv.exe /Edit <pathToFolder> or <path to VS>devenv.exe <path to folder>
I am forced to use VS2017, VS2019, and VS2022.
I added the path to my environment vars and had consistent success where using the full path in a literal would fail.
Since I work primarily from PowerShell I made this function for my needs. This uses the associated version of visual studio by invoking the solution [.sln] file. My .sln files are associated with the 'Microsoft Visual Studio Version Selector' If you only have one VS installed it still works as your sln files [should] be associated with your VS.
function Open-Solution {
<#
.SYNOPSIS
Open solution file for visual studio
.DESCRIPTION
Open proper version of visual studio
-RootDirectory
folder to search for solution in or to open in VS
<Usage>
PS C:\>sln
- Open folder or solution in the active directory
PS C:\>sln .\myApp
- Open folder or solution in the .\myApp directory passed in the parameter
PS C:\>sln -path .\myApp
- Open folder or solution in the .\myApp directory passed in the parameter
#>
[Alias('sln')]
param (
[Alias('path')]
[Alias('searchpath')]
[string]$RootDirectory = $PWD
)
$solutions = Get-ChildItem -Recurse -Path $RootDirectory -Filter "*.sln"
if ($solutions.Count -eq 1) {
Invoke-Item $solutions.FullName
} elseif ($solutions.Count -eq 0) {
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes, Open current folder', "Open [$RootDirectory] in VS";
$No = New-Object System.Management.Automation.Host.ChoiceDescription '&No', "Do Not Open [$RootDirectory]";
$options = [System.Management.Automation.Host.ChoiceDescription[]]($Yes, $No);
$title = "No solution files found searching [$RootDirectory]`n`n";
$message = "Would you like to open $RootDirectory in Visual Studio?";
$result = $host.ui.PromptForChoice($title, $message, $options, 1);
switch ($result)
{
0
{
Write-Host Opening [$RootDirectory] in VS -ForegroundColor green;
Invoke-Command {devenv.exe $RootDirectory };
break;
}
1
{
break;
}
default
{
break;
}
}
} elseif ($solutions.Count -gt 1) {
#TODO: allow selection deal with subfolders. (Doubt juice worth squeeze)
Write-Host "`nI found more than 1 solution. Which one do you want to open?" -ForegroundColor Yellow;
$solutions | Format-Table #{ Label="Solutions"; Expression={" --> $_"} }
}
}
I hope this adds value to your work stream.
Cheers
I don't know if this could be helpful but this might be the way to open a folder in visual studio.
go to the directory you want to open in visual studio in cmd/terminal
type explorer .
it will open the same directory in the file explorer
then right click and choose open with visual studio
This is what I do as a shortcut:
Navigate to the folder, ex. "c:\project".
Click on the navigation bar, as you would change location. Then type "cmd"
CMD will prompt to this directory
Type "code .", hit enter.
VBS should now load project folder.
In windows command line navigate to your project folder and type this command:
devenv start .

Strange (c++) debug issues using VS2013

I have inherited a project to work on and the initial build was developed on linux. I dont know if this matters or not but thought I would share it.
In order to debug the project on a windows machine I first use the CMakeGUI on win7 to create a Visual Studio Solutions file to open the project using Visual Studio 2013 and then set the startup project and build the project I am interested in. Up till now everything is okay. Now comes the confusing part.
On load the program is suppose to read a file lets call it in.dat and is declared in const char * inputFileName this variable is then passed through a class which attempts to open then file to obtain data.
fstream fs;
fs.open(inputFileName.c_str(), fstream::in);
if(!fs.is_open())
{
std::cout << "Cannot open input file!" << std::endl;
exit(0);
}
This where I am stumped...the file when placed in the debug folder for some reason cannot be opened i.e fs.is_open() returns false when I try debugging the application BUT if I cd directly into the debug folder of the project, outside of VS, and run the executable it runs as expected i.e fs.is_open() now returns true.
Your debugger's working directory defaults to your project's root directory and the binary is in the \debug subdirectory, resulting, in effect, to the path to the input file being wrong.
Set the debugger's working directory to \debug.
Here is more info on that:
https://msdn.microsoft.com/en-us/library/kcw4dzyf(v=vs.120).aspx

ifstream can't find file that is saved in the directory

I'm writing a program that uses <fstream>. The file I need to include is called employee.dat and it's listed in the directory. I put in a cout statement that says:
ifstream inFile ("EMPLOYEE.DAT");
if (! inFile)
{
cout << "!!Error in opening 'EMPLOYEE.DAT"<< endl;
}
The file is in the directory and there shouldn't be any issues opening/finding it. This is my first time using the ifstream class.
If you are running from the Visual Studio environment, be aware that your executable probably isn't in the same directory as your data file. Binaries tend to be built into a Debug or Release folder by default. You have several options:
Move your data file into the correct directory.
Set the Working Directory (in your project settings, under "debugging").
Use a full or relative path for your file name.
It's also possible that you are looking in the right place, but the file is locked. Make sure it's not open in any editor or other program that might prevent other processes from opening it.
Beware that in Visual Studio, the Working Directory (set in the project properties under the Debugging tab) is the location that the exe is apparently located when debugging (and not the actual location of the exe itself)

Why can't I read a file with Code::Blocks C++?

I've created a file in Code::Blocks called datos.csv, and I have this code:
std::ifstream file("datos.csv");
if (file) {
cout << "Managed to read file successfully.";
}else{
cout << "Unable to read file.";
}
But it is unable to read the file.
I tested the same code with TextMate, which can run C++ files, and it was indeed able to read the file, so I suppose there's something up with Code::Blocks. What am I missing?
My file appears listed in "Others" in Code::Blocks' navigator.
you need to modify the Target Properties, go to Project -> Properties -> Build targets and change the "Executing Working Dir" for the debug/release folder of your proyect, I hope this help.
Greetings.
Saludos.
It can't find the file to open it. Since you are not using absolute paths to open the file it must be relative to the current working directory. If you are launching from the debugger you can set the working directory used when the application is launched. Make sure that directory is the same as where the csv file is located.