在 C 中打开应用程序 [英] Open Application In C

查看:28
本文介绍了在 C 中打开应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用 C 语言的简单命令打开应用程序?

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

这是我当前的代码:

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

推荐答案

你的程序崩溃的原因是-

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 

input 声明为数组 -

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' 

这 - system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"); 应该写成 -

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

这个 -

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

是行不通的.使用strcmp比较字符串-

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

if (strcmp(input,"Open Google Chrome") == 0)    // similarly compare other strings.

这篇关于在 C 中打开应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆