Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Application In C

Tags:

c

windows

Is there a way I can open an application from a simple command in C?

This is my current code:

int main()
{
    char input;

    printf("Hello! How may I help you? \n");
    scanf("%s", input);

    if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome"){
        printf("Sure! Opening Google Chrome...");
        system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
        printf("Done!");
    }

    return 0;
}

But all it does is tell me that my program has stopped working and quits...


2 Answers

Reason your program crash is this -

char input;              // char variable can hold single character
printf("Hello! How may I help you? \n");
scanf("%s", input);             // %s expects char * you pass a char 

Declare input as an array -

char input[100];
...
fgets(input,100,stdin);    // don't use scanf as before suggested  
char *position;
if ((position = strchr(input, '\n')) != NULL)     
  *position= '\0';                             // to remove trailing '\n' 

And this - system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"); should be written as -

system("Start C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

This -

if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google 

is not going to work . Use strcmp to compare strings -

if (strcmp(input,"Open Google Chrome") == 0)    // similarly compare other strings.
like image 105
ameyCU Avatar answered Dec 06 '25 08:12

ameyCU


Your code has several problems:

  1. You have char input; which can store a single char. But you want to store a string. So, change that into an array of some size:

    char input[64];
    
  2. You have scanf("%s", input);. %s stops on encountering a whitespace character. In order to read spaces upto a newline character('\n'), use the %[ format specifier:

    scanf("%63[^\n]%*c", input); /* %*c is not required; It discards the \n character */
                                 /* %[^\n] scans everything until a '\n' */
                                 /* The 63 prevents Buffer overflows */
                                 /* It is better to check scanf's return value to see if it was successful */
    
  3. You have if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome"){. This doesn't do what you expect. It compares whether the address of the first element of input has the same address of that of the string literals. Also, chaining OR operators like that doesn't do what you expect.

    You'll need to include string.h and use strcmp:

    if(strcmp(input, "Open Google Chrome") == 0 || strcmp(input, "open google chrome") == 0 || strcmp(input, "open chrome") == 0 || strcmp(input, "run google chrome") == 0 || strcmp(input, "run chrome") == 0 || strcmp(input, "Run Google Chrome") == 0 || strcmp(input, "Run Chrome") == 0 || strcmp(input, "Open Chrome") == 0){
    
  4. Also, as other people have pointed out, you need to escape the \ character in the system function by using \\ instead of \. You might need to enclose the whole thing in double quotes("...") too.
like image 38
Spikatrix Avatar answered Dec 06 '25 09:12

Spikatrix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!