C++ on Windows: executable path with whitespace in system() call - c++

I am trying to execute a file with parameters using the "system()" function in C++ on Windows, and it works as long as there are no whitespaces in the filename. For parameters, putting double quotes around the string works, but when I try the same with the executable itself, I get the following error:
"the filename,directory name, or volume label syntax is incorrect"
Does anyone know how to handle this correctly?

Use a string like this:
cmd /S /C "your entire command line string"
See: How do I deal with quote characters when using cmd.exe

It should work, look for the problem elsewhere.
Perhaps something in your flow is removing the whitespace or the double quotes from the string.

Related

Pass \ in command line arguments of C++

I am working on a C++ program where one of the command line arguments needs to be a passed a regex. For example: abc.exe --option ab\[0\]
When I access the option value from inside the program, it becomes ab\\[0\\] which becomes a different regex.
Inside the program when I try to replace \\[ with \[ using boost::replace_all, the result is [ which also is not the intended output for me.
So, any suggestions on how to pass and retain \[ this while passing it through command line arguments
You can quote the parameter:
abc.exe --option "ab\[0\]"
Or use the shell escape sequence:
abc.exe --option ab\\[0\\]
Did you try these?
It was a problem with how visual studio displays the symbol. When I looked on the ASCII code of the symbol, it was alright. Thanks #ScottK for helping me to debug this through your comments

How do I open a file within notepad++ with C++?

I'm trying to get my c++ program to open an sql file in notepad++. I can get it to open with notepad like this:
system("notepad.exe script_foo.sql");
But that's undesirable as it's not formatted. When I try to substitute notepad.exe for notepad++.exe like this:
system("'C:\Program Files\Notepad++\notepad++.exe' script_foo.sql");
I get a invalid syntax error.
Any issues where I'm going wrong?
The WinNT shell uses double-quotes to include spaces in a file name. Single quotes are not recognized. So you need
"C:\Program Files\Notepad++\notepad++.exe" script_foo.sql
as your command.
To embed this in C++ source code, you'll need to escape backslashes (as Andre already mentioned) and also the double-quotes.
system("\"C:\\Program Files\\Notepad++\\notepad++.exe\" script_foo.sql");
In C++, the backslash character \ is an escape character in strings. You need to double the backslashes to achieve what you really want:
system("'C:\\Program Files\\Notepad++\\notepad++.exe' script_foo.sql");

C++ Command Line Argument Identification

I need to be able to tell if the final argument in my command line is surrounded in double quotes or not. If it's in double quotes, I treat it as a string. If it's not, I need to treat it as a file to open and obtain the string. Argv by default will grab the double quoted string and strip the quotes, so I can't figure out a way to handle this problem.
pseudocode is something like this...
if(argv[argc-1] was called with surrounding double quotes){
//handle as string (I already have code to do this)
}
else{
//handle as filename (I already have code to do this)
}
All of the parameters in argv are strings. You are probably better off rethinking your strategy. Try opening the argument, if that fails then treat it as a string.
Alternatively you could escape the quotes on the command line and they will be passed to your application:
$ program "\"this is a string\""
Edit: The sample code assumes you are using a Bash shell or something similar

executing filenames with spaces in cmd pmt Passed from c++ program

I am currently working on getting my program to execute a program (such as power point) and then beside it the path to the file I want to open. My program is getting the file's path by using:
dirIter2->path()
I get the 2 paths of the program and file, Merge them as one string and pass them into the following:
system(PathTotal.c_str())
this is working great but my only issue is that when the file name has a space in its name command prompt says it cannont find the file (becuase it thinks the file name ends when it gets to the first space. I have tried to wrap it with quotes but it is the acutal file name that need to be wrapped.
(eg. i have tried "C:\users\bob\john is cool" but it needs to be like this: C:\users\bob\"john is cool")
Does anyone have any suggestions on how I could fix this? I was thinking about getting the path to the folder to where the file and then getting the file name. I would wrap the file name with quotes then add it to the folder's path. I have tried using the ->path() like above but the only problem is that it only goes to outside of the folder's directory?
Is there a boost command that could get the enitre path to the file without getting the file aswell?
I am not commited to this idea if anyone has any better suggestions
Thanks
In both C and C++, the '\' is an escape character. For certain things (like '\n' or '\t') it inserts a control code; otherwise, it just gives you the next character.
So if you do something like:
fopen("C:\users\bob\john is cool", "r");
it's going to try to open a file named
C:usersbobjohn is cool
If you want those '\' characters in the output, you have to escape them. So you'd want:
fopen("C:\\users\\bob\\john is cool", "r");
On Windows with Visual Studio, I've also successfully used Unix-style separators:
fopen("C:/users/bob/john is cool", "r");
And in fact, you can mix them up:
fopen("C:/users\\bob/john is cool", "r");
I'm not familiar with C string operations, but couldn't you do the following rather easily?
int i = path.lastIndexOf("\\"); //Find the index of the last "\"
String quotedPath = path.substring(0, i+1); //Get the path up until the last "\"
quotedPath += "\"" + path.substring(i+2) + "\""; //Add quotes and concatenate the filename
Sorry for the Java, its the closest thing that I'm familiar with. I've made this a community wiki in case someone can edit the code to the equivalent C.
I'd also like to add that sometimes it is necessary to escape spaces as in the following:
cmd.exe -C C:/Program\ Files/Application\ Folder/Executable\ with\ spaces.exe
or
cmd.exe -C C:\\Program\ Files\\Application\ Folder\\Executable\ with\ spaces.exe

Devenv.exe with /I and whitespacess

I've tried to compile an application with Directx. But this causes an PRJ0030 error for $().
How can I escape critical characters like (,) or blanks. Refering to the cmd I've used ^ but it does not help.
AdditionalLibraryDirectories=""$(DIRECTX_ROOT)\Lib\x86""
&quot should not be there. $(DIRECTX_ROOT) requires the macro to be set in a project property sheet. You are better off spelling it out:
AdditionalLibraryDirectories="c:\blah\dx9\Lib\x86"
cmd.exe unfortunately isn't a proper shell like bash, and parsing the command line is up to each individual program. I can't speak for devenv.exe but a common convention is to surround troublesome strings with double quotes (").