error when executing dos command

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

error when executing dos command

 
0
  #1
May 14th, 2007
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

  1. string x;
  2. x = "\"c:\\folder\\file.exe\" -parameter1:value1 -parameter2:value2";
  3. 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

  1. string x;
  2. x = "\"c:\\folder\\file.exe\" -parameter1:\"value1\"";
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #2
May 14th, 2007
do you need the quotes around the value?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: error when executing dos command

 
0
  #3
May 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #4
May 14th, 2007
Do you think it is a problem with escape characters then?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #5
May 14th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #6
May 14th, 2007
Just on a side note, some people would frown on system commands. The alternative is:-

click_me

But it is a little more complicated.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: error when executing dos command

 
0
  #7
May 14th, 2007
Originally Posted by iamthwee View Post
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.
ofcourse i did that , because i thought something might be wrong with my code ,

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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: error when executing dos command

 
0
  #8
May 14th, 2007
Originally Posted by iamthwee View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #9
May 14th, 2007
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");
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: error when executing dos command

 
0
  #10
May 14th, 2007
>link isn't working
Yes that is odd the content was:-

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <direct.h>
  5.  
  6. int main( )
  7. {
  8. chdir("C:/j2sdk1.4.2_04/bin");
  9.  
  10. STARTUPINFO si;
  11. PROCESS_INFORMATION pi;
  12. LPTSTR szCmdline=_tcsdup(TEXT("javac Saluton.java"));
  13.  
  14. ZeroMemory( &si, sizeof(si) );
  15. si.cb = sizeof(si);
  16. ZeroMemory( &pi, sizeof(pi) );
  17.  
  18. // Start the child process.
  19. if( !CreateProcess( NULL, // No module name (use command line)
  20. szCmdline, // Command line
  21. NULL, // Process handle not inheritable
  22. NULL, // Thread handle not inheritable
  23. FALSE, // Set handle inheritance to FALSE
  24. 0, // No creation flags
  25. NULL, // Use parent's environment block
  26. NULL, // Use parent's starting directory
  27. &si, // Pointer to STARTUPINFO structure
  28. &pi ) // Pointer to PROCESS_INFORMATION structure
  29. )
  30. {
  31. printf( "CreateProcess failed (%d).\n", GetLastError() );
  32. getchar();
  33. //return 0;
  34. }
  35.  
  36. // Wait until child process exits.
  37. WaitForSingleObject( pi.hProcess, INFINITE );
  38.  
  39. // Close process and thread handles.
  40. CloseHandle( pi.hProcess );
  41. CloseHandle( pi.hThread );
  42.  
  43. getchar();
  44. }

Which is an alternative to system calls on the windows platform...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC