943,794 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6813
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 14th, 2007
0

error when executing dos command

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  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

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
May 14th, 2007
0

Re: error when executing dos command

do you need the quotes around the value?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 14th, 2007
0

Re: error when executing dos command

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
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
May 14th, 2007
0

Re: error when executing dos command

Do you think it is a problem with escape characters then?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 14th, 2007
0

Re: error when executing dos command

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 14th, 2007
0

Re: error when executing dos command

Just on a side note, some people would frown on system commands. The alternative is:-

click_me

But it is a little more complicated.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 14th, 2007
0

Re: error when executing dos command

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
May 14th, 2007
0

Re: error when executing dos command

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
May 14th, 2007
0

Re: error when executing dos command

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");
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 14th, 2007
0

Re: error when executing dos command

>link isn't working
Yes that is odd the content was:-

C++ Syntax (Toggle Plain Text)
  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...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading file path given by user
Next Thread in C++ Forum Timeline: Issues when placing functions from template class into seperate .cpp file [C++]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC