system () call... to open mybatchfiles.bat - c++

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).

Related

Passing a angled bracket as a command-line argument input while debugging

I'm writing a brainfuck interpreter in NASM, where code is supplied as a command line argument to the program. I'm trying to test looping, but GDB doesn't like my input. For example, this executes error-free when run on its own:
$./interpret "+++++[->+<]"
It hangs indefinitely, but I think that that's due to a bug in the looping logic in the interpreter (thus GDB).
If I load interpret into GDB though and attempt to supply the same argument, I get complaints:
gef➤ start "+++++[->+<]"
/bin/bash: line 1: ]: No such file or directory
/bin/bash: line 1: ]: No such file or directory
This seems to be due to < being interpreted as redirection despite the quotes, since [] works fine in GDB.
I tried escaping the STDIN redirection with \<, but that leads to the same error, and <<, but that leads to a warning:
gef➤ start "+++++[->+<<]"
/bin/bash: line 1: warning: here-document at line 1 delimited by end-of-file (wanted `]')
And the code gets cut off:
$r15 : 0x00007fffffffe428 → 0x002d5b2b2b2b2b2b ("+++++[-"?)
Is there a way to have GDB take what I give literally to start, and not attempt to do any redirection/interpretation of the arguments?
Is there a way to have GDB take what I give literally to start, and not attempt to do any redirection/interpretation of the arguments?
GDB isn't doing any interpretation, bash does. Using single-quotes instead of double-quotes may fix that.
(I wasn't able to replicate the problem using GDB-10.0 and bash-5.1.4 with double quotes though.)

Pass string with spaces as system argument 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());

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" ...

How to properly use system() to execute a command in C++?

I am new to C++ programming under Windows. I am trying to execute a command say cuobjdump in C++ code using the system() function:
system("C:\\program files\\nvidia gpu computing...\\cuobjdump.exe --dump-cubin C:\\..\\input.exe");
output:
Usage : cuobjdump [options] <file>
This followed by the list of the options for cuobjdump.
When I execute this program I always get the cuobjdump help options displayed in the command line. It's as if the system call does not parse the filename. What am I doing wrong? I get the same result while using createprocess. The options --dump-cubin gives an error as if I mistyped it.
Give a try with (that is, surrounding cuobjdump.exe path with ", properly escaped in C++ as \"):
system("\"C:\\program files\\nvidia gpu computing...\\cuobjdump.exe\" --dump-cubin C:\\..\\input.exe");
system("cuobjdump --dump-cubin path\filename.exe");
That \f is interpreted by the compiler as a string escape sequence, try path\\filename.exe
Most obviously, \ is an escape character in C / C++ strings, so it has to be doubled if you want to use it literally.
system("cuobjdump --dump-cubin path\\filename.exe");
Assuming that path is correct, you have to use a double \\ within strings to represent a single \.
I suggest you to use CreateProcess, or ShellExecute / ShellExecuteEx since you are working on Windows. system and ShellExecute eventually calls CreateProcess only.

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.