How to Properly Use System() to Execute a Command in C++

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

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

How many ways are there to execute system command in C program for windows

Standard C libraries give you only one way to execute external command in OS, so use int system(const char *command).

You can save output of this command to text file, and then read this file from you program.

For example:

#include <stdio.h>
#include <stdlib.h>

#define TMP_FILE_NAME "TMP_FOLDER_CONTENT.txt"

int main(int argc, char *argv[])
{
system("dir C:\* > "TMP_FILE_NAME);
FILE * fdir = fopen(TMP_FILE_NAME, "r");
char buff[100];
if (fdir)
{
while (1) {
if (fgets(buff, 100, fdir) == NULL) break;
printf("%s", buff);
}
}
fclose(fdir);
remove(TMP_FILE_NAME);
return 0;
}

Where dir is a program to be executed, C:\* - argument of the program, and > - redirection of standard output for that command after which filename TMP_FOLDER_CONTENT.txt will be substituted.

Also you can check returned value, as:

int errorcode = system("dir C:\* > "TMP_FILE_NAME);
printf("Command executed and returned a value %d\n", errorcode);

or taking into account command you use, change the logic of your program, e.g.:

int errorcode = system("dir C:\* > "TMP_FILE_NAME);
if( errorcode )
{
return errorcode;
}

UPDATE:

Alternatively, you could use pipes in C++, for example as shown in the answer to question How to execute a command and get output of command within C++ using POSIX?

How to execute an external program in c

The are several problems is the string "start C:\Riot Games\League of Legends\LeagueClient.exe".

First of all, the \ character is used to escape characters, which means enter characters that would mean something else if they were inserted directly in the string. For example, to write " in a string, you should use \" since " on its own would mean that it's the end of the string. Similarly, \n means a line break since you can't write a line break directly in the string. Here, \R in C:\Riot Games means that you are escaping the character R which doesn't mean anything. The compiler interprets \R as a simple R (the same holds for \L) and therefore converts the string "start C:\Riot Games\League of Legends\LeagueClient.exe" to "start C:Riot GamesLeague of LegendsLeagueClient.exe". To escape the \ character, use \\. So so far, the string should be system("start C:\\Riot Games\\League of Legends\\LeagueClient.exe").

There is also another problem in that string, it's that a space in a command line usually means that you run the program with the path before the space with the parameters specified after the space. This is usually used to open a file with a program. To make it simple, "start C:\\Riot Games\\League of Legends\\LeagueClient.exe" means that you want to run the program C:\Riot and make it open the file Games\League of Legends\LeagueClient.exe. And as we said previously, the compiler turns the C:\Riot in your code to C:Riot, so it's trying to run the program C:Riot and of course it can not find it, so it gives you the error that you mentioned. Anyway, to tell the computer that the spaces are actually part of the file name, you have to put quotation marks " around the file name. As I said previously, to use quotation marks in strings, use \". So the correct way to do would be system("start \"C:\\Riot Games\\League of Legends\\LeagueClient.exe\""). Also, start opens the console, so if you would like to open the program itself, use simply system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"").

Another problem with your code is that true isn't defined in C. So you should either use while(1) or define true as a macro using #define true 1.

So a correct code would be the following:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define _WIN32_WINNT 0x0500
#include <windows.h>

#ifndef true //test if true is defined in case it's already defined somewhere else
#define true 1
#endif

int main () {
POINT mouse;
//HWND hWnd = GetConsoleWindow();
//ShowWindow(hWnd, SW_MINIMIZE);
//ShowWindow(hWnd, SW_HIDE);
// how to open program ?
system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"");

while (true) {
GetCursorPos(&mouse);
int x = mouse.x;
int y = mouse.y;
SetCursorPos(x + rand() % 40 - 20, y + rand() % 40 - 20);
printf("x = %d ", mouse.x);
printf("y = %d\n", mouse.y);
Sleep(1);
}
}


Related Topics



Leave a reply



Submit