Pass string with spaces as system argument c++ - c++

If I have a path(string) with spaces, for example "C:\\Users\\Irina\\Desktop\\POO\\PROIECT POO\\my file.docx" and I want to open that file, I use the system command like this
system(path.c_str())
And I get the following error :
'C:\Users\Irina\Desktop\POO\PROIECT' is not recognized as an internal or external command,
operable program or batch file.
I would really appreciate some help.
Thanks :)

You need to enclose the string in double quotes:
system(('"' + path + '"').c_str());

Related

BuildForge - Passing variables as parameters that are declared as Assing Hidden

I have a build forge job that executes a bat file and intake password as a paramter. If i declare this paramter variable as normal text it works fine. But when I declare password as "Assing Hidden" it doesnt work and throws out error as meniotned below.
Any help is appreciated.
Command I am executing
call MpToSbx.bat SF %account% %password% %REL_NUM% %Track%
Condition
1. Works fine if password is delcared as regular text.
2. When password assigned as hidden it throws out error as below.I feels the command is not iterated correctly some how.
Error Message:
The system cannot find the path specified.
EXEC 'MpToSbx.bat' is not recognized as an internal or external command,
EXEC operable program or batch file.
The most probable cause of this error is that %password% is taken as a single word inside MpToSbx.bat file. To solve this error, first edit that file and change %2 by %~2 (to eliminate quotes in the parameter) and then enclose the password between quotes in the call; for example:
call MpToSbx.bat SF %account% "%password%" %REL_NUM% %Track%

Reading logical operator from command line

I am trying to read 12|11|13 from command line from my application.
I get error "11 is not recognozed as an internal command" while reading this string.
I am OK if I can read complete string or individual numbers.
Can anyone suggest how to read this?
Nipun
You need to pass the args between quotes ex :
myprog "11|12|14"
Because if you don't, the caracter | (pipe) is used to pass the output from the program on left to the program on right of the symbol.

system () call... to open mybatchfiles.bat

I am getting the following error when using the system() call to run a batch file:
'C:\newfldr\mybatchfiles.bat' is not recognized as internal or external command, operable program or batch file.
error at : system("C:\newfldr\mybatchfiles.bat");
Can anyone explain why?
You very probably should write your string correctly, eg:
system("C:\\newfldr\\mybatchfile.bat");
Remember that the C compiler is interpreting back slash escapes in constant string literals. In particular \n is a newline character, and \\ encodes a backslash.
You could have printed your command string for debugging to find the issue (or use a debugger).
BTW, on Linux you don't have that issue because \ is rarely used in file paths. Did you consider trying Linux? (it is fun).

popen and system behaves unexpectedly with multiple quoted file paths

I am trying to execute a dos command from within my C++ program, however soon as I add quotes to the output filepath (of a redirection) the command no longer gets executed and returns instantly. I've shown an example below of a path without spaces, but since paths may have spaces and thus be quoted for the shell to understand it properly I need to solve this dilemma - and I'm trying to get the simplest case working first.
i.e.
The following WORKS:
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > C:/backup.bak",user,password,db_name);
system(exec_cmd);
The following does NOT work (notice the quotes around the output):
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > \"C:/backup.bak\"",user,password,db_name);
system(exec_cmd);
I'm guessing it is choking somewhere. I've tried the same "exec_cmd" in popen to no avail.
Any help/advice is greatly appreciated.
I don't think your shell (cmd.exe) allows redirection to a file name with spaces. I couldn't make my command.com from DOS 6.22 accept it (I don't have a cmd.exe nearby to test).
Anyway, you can use the --result-file option to pass the redirection to the command itself.
mysqldump ... --result-file="file name" ...

System function seems to ignore quote marks

I'm running the following code (on Windows 7, if it makes a difference):
char temp[20000];
sprintf_s(temp, 20000, "\"C:\\Program Files\\Mozilla Firefox\\firefox.exe\" \"http://www.tvtak.com/servlet/Gateway/?C=addShows&channel=%s&show=%s\"", _channels[chId], name);
system(temp);
On running, the console displays:
'C:\Program' is not recognized as an
internal or external command, operable
program or batch file.
'channel' is not recognized as an internal or
external command, operable program or
batch file.
'show' is not recognized
as an internal or external command,
operable program or batch file.
But when I get the value of 'temp' via QuickWatch and paste it to CMD, it works fine. What's going on here?
I should mention that the parameters I'm appending to the string contain non-latin characters. Could this have something to do with it?
You are better off with CreateProcess() to avoid cmd.exe's quoting hell. But if you must use system() you can simply append if 1==1 to the beginning of your command so it doesn't remove the quotes for you.
system("if 1==1 \"C:\Program...");
For more information about this issue, run cmd.exe /? and look for /S.
You need to put entire string into yet another pair of quotation marks. And try to avoid C-style strings in C++ code.