| | |
error when executing dos command
Thread Solved
![]() |
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
hello ,
first i want to say that im very new at c++ , i managed to create a very simple program through searching the internet , this little program involves running a certain exe file with parameters :
(e.g: "c:\folder\file.exe" -parameter1 -parameter2)
i am using the system() function to do that so my code looks something like
so it would run this command ( "c:\folder\file.exe" -parameter1:value1 -parameter2:value2 )
it works fine untill one of the parameters values must include quotes in it like this ( -parameter:"value" )
so when i type the code like this
i get the error :
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
but if i put the same command line ( with the quoted value of the parameter ) in the windows run , or in CMD , it would work fine, why do i get the error when i try to run it from the system() function ..?!!
i appereciate any help.
first i want to say that im very new at c++ , i managed to create a very simple program through searching the internet , this little program involves running a certain exe file with parameters :
(e.g: "c:\folder\file.exe" -parameter1 -parameter2)
i am using the system() function to do that so my code looks something like
c++ Syntax (Toggle Plain Text)
string x; x = "\"c:\\folder\\file.exe\" -parameter1:value1 -parameter2:value2"; system(x.c_str());
so it would run this command ( "c:\folder\file.exe" -parameter1:value1 -parameter2:value2 )
it works fine untill one of the parameters values must include quotes in it like this ( -parameter:"value" )
so when i type the code like this
c++ Syntax (Toggle Plain Text)
string x; x = "\"c:\\folder\\file.exe\" -parameter1:\"value1\""; system(x.c_str());
i get the error :
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
but if i put the same command line ( with the quoted value of the parameter ) in the windows run , or in CMD , it would work fine, why do i get the error when i try to run it from the system() function ..?!!
i appereciate any help.
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
yes ,
using the system function ,
this command runs fine : ("c:\folder\file.exe" -parameter1:value1)
but some values contain spaces so i would then have to include them between quotes like this :
( "c:\folder\file.exe" -parameter1:value1 -parameter2:"value with spaces" )
this command gives me error when i run it from the system function , if i removed the quotes from the value , the commads line works , but ofcourse i would get a different error from that exe its self because quotes need to be added to the parameter value.
if i copied the same line and pasted it in the RUN window or a CMD window , it would work fine
using the system function ,
this command runs fine : ("c:\folder\file.exe" -parameter1:value1)
but some values contain spaces so i would then have to include them between quotes like this :
( "c:\folder\file.exe" -parameter1:value1 -parameter2:"value with spaces" )
this command gives me error when i run it from the system function , if i removed the quotes from the value , the commads line works , but ofcourse i would get a different error from that exe its self because quotes need to be added to the parameter value.
if i copied the same line and pasted it in the RUN window or a CMD window , it would work fine
Just on a side note, some people would frown on system commands. The alternative is:-
click_me
But it is a little more complicated.
click_me
But it is a little more complicated.
*Voted best profile in the world*
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
What I would do is a cout of
"\"c:\\folder\\file.exe\" -parameter1:\"value1\""
Just to see if it is identical to the command you paste in the cmd window.
the "cout" gives the correct command line in the right form ,
since there should be quotes around the path to the exe file that i need to run , maybe adding two sets of quotes in the same string (x) is causing the problem , i tried to create two strings to seperate (-parameter:"value with quotes") into another string say (Y)
then tried adding the two strings into a third new one (Z)=(X)+(Y)
and applied it to the function " system(Z.c_str()) " , but that didnt change anything , and i dont know how to add the two strings inside the function itself , because i end up with the error :
" cannot add two pointers " when i try to compile
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
Just on a side note, some people would frown on system commands. The alternative is:-
click_me
But it is a little more complicated.
actually i'm using the system function because the command i need to run is very simple , i never thought that a couple of quotes would ruin a perfectly working command line

that link isnt working :-(
Last edited by hashinclude; May 14th, 2007 at 4:16 pm.
Well from what you have said, i.e the cout is exactly the same as the one you pasted in the command line, I can only assume there is a problem with the system command.
Can't you create a bat file with that exact command and just call the bat file i.e
system("something.bat");
Can't you create a bat file with that exact command and just call the bat file i.e
system("something.bat");
*Voted best profile in the world*
>link isn't working
Yes that is odd the content was:-
Which is an alternative to system calls on the windows platform...
Yes that is odd the content was:-
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <stdio.h> #include <tchar.h> #include <direct.h> int main( ) { chdir("C:/j2sdk1.4.2_04/bin"); STARTUPINFO si; PROCESS_INFORMATION pi; LPTSTR szCmdline=_tcsdup(TEXT("javac Saluton.java")); ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line) szCmdline, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Pointer to PROCESS_INFORMATION structure ) { printf( "CreateProcess failed (%d).\n", GetLastError() ); getchar(); //return 0; } // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); getchar(); }
Which is an alternative to system calls on the windows platform...
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: Reading file path given by user
- Next Thread: Issues when placing functions from template class into seperate .cpp file [C++]
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






