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

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

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.

6
Contributors
22
Replies
23
Views
9 Years
Discussion Span
Last Post by epitalon
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

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.

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.

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 :(

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 :-(

0

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");

0

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

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

0

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");

i can't because one of the parameter's value is a variable inside my program , unless there is a way (or function) to create a new bat file and write the final command line inside it and then run that bat file O_o , which if was possible , i wouldn't know how to pull that off :(

0

That would be possible, all you would be doing is writing a file with a .bat extension then calling it as a system process.

0

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

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

i'll have to spend sometime trying to figure how this one works (i'm new at this) , it is more complicated , but thanks :)

0

Which compiler are you using?

If it's something particularly old like Turbo C 2.01, then you're probably not using cmd.exe as your command interpreter, but the rather more archaic command.com.

0

That would be possible, all you would be doing is writing a file with a .bat extension then calling it as a system process.

i don't know how to do that with system ()

0

Which compiler are you using?

If it's something particularly old like Turbo C 2.01, then you're probably not using cmd.exe as your command interpreter, but the rather more archaic command.com.

i'm using microsoft VC++

0

If you know how to write variables to a file. Then you can write your command to a bat file then just call it...

system("something.bat");

Try it. I'll leave it with you.

0

lol , thinking about , even if i could do it , having to create a bat file just to execute the command is not going to be useful for me

thanks for your help :)

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

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

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.

Check the quotes in the line you posted, there are too many of them. -- remove the quotes at the beginning of the string and after file.exe. It should be something like this (I tested it and it works)

system("\\dvlp\\file.exe -parameter1:value1 -parameter2:\"value2 with spaces\"");
0

thanks Ancient Dragon ,
but your solution didn't solve the problem , i still get the same error.
is there a way to run the same command line other than the system() function ?

0

I'm sorry I didn't see your question before now. If you are still having a problem with this please post your most recent attempt to solve it.

0

one suggestion :
try enclosing the whole "-parameter2: parameter value"
in quotes

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.