【问题标题】:Open Application In C在 C 中打开应用程序
【发布时间】:2015-09-27 10:19:35
【问题描述】:

有没有一种方法可以通过 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;
}

但它所做的只是告诉我我的程序已经停止工作并退出......

【问题讨论】:

  • 是什么让你认为input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome" 会与input 比较字符串?
  • 不比较;但看看它是哪一个。我习惯了 php,我注意到 php 很像 C,所以我认为它会工作
  • \ 在启动命令字符串中应该是 `\`
  • \ 应该是 \\ 格式让我明白了

标签: c windows


【解决方案1】:

你的程序崩溃的原因是这样的 -

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"); 应该写成 -

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

这-

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

不会工作。使用strcmp 比较字符串-

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

【讨论】:

  • @iharob 先生,这不是问题。随时欢迎提出建议和改进:)
  • @iharob 好的,所以我只是运行它,一切正常,但是当我说“open chrome”时,它想出了The system cannot find C:Program?这是怎么回事?
  • 您必须像 \\ 一样转义 \ 字符。请启用编译警告,我不知道您使用的是什么编译器,但它们都有警告和诊断。
  • @iharob 我正在使用 GNU
  • @CaelanGrgurovic 然后编译时使用警告标志启用-Wall -Wextra
【解决方案2】:

你的代码有几个问题:

  1. 您有char input;,它可以存储单个 char。但是你想存储一个字符串。因此,将其更改为某个大小的数组:

    char input[64];
    
  2. 你有scanf("%s", input);%s 在遇到空白字符时停止。要读取最多换行符的空格 ('\n'),请使用 %[ 格式说明符:

    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. 你有if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome"){。这不符合您的预期。它比较input 的第一个元素的地址是否与字符串文字的地址相同。此外,像这样链接 OR 运算符并不能达到您的预期。

    您需要包含string.h 并使用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. 另外,正如其他人所指出的,您需要使用\\ 而不是\ 来转义system 函数中的\ 字符。您可能还需要将整个内容用双引号 ("...") 括起来。

【讨论】:

  • 非常感谢!这正是我想要的! :D
【解决方案3】:

你的代码有一些问题,其他人已经指出了,所以我不会。但是,我想发布我将如何去做你想做的事情。

这里是……

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

int
main (void)
{
    char input[100];

    char *chrome_answers[] = {
        "Open Google Chrome",
        "open google chrome",
        "open chrome",
        "run google chrome",
        "run chrome",
        "Run Google Chrome",
        "Run Chrome",
        "Open Chrome"
    };

    int chrome_length = sizeof (chrome_answers) / sizeof (*chrome_answers);
    int i;

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

    for (i = 0; i < chrome_length; i++) {
        if (strcmp (input, chrome_answers[i]) == 0) {
            printf ("Sure! Opening Google Chrome... ");
            fflush (stdout);
            system ("Start C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
            printf ("Done!\n");

            break;
        }
    }

    return 0;
}

主要是我会使用一组可能的有效“答案”,而不是像您在代码中那样使用很长的“if 语句”,当您根据需要使用 strcmp 时,它会变得更长。

【讨论】:

    【解决方案4】:

    你不能那样做。一个字符应该只包含一个字符,如“s”或“t”或“5”。 “打开谷歌浏览器”是一串字符。例如,如果要将字符串输入与“open google chrome”进行比较,则需要创建一个这样的数组:char[10] input;例如,它将创建一个可以包含 10 个字符的数组。然后比较每个字符,您需要执行以下操作: char *str = "打开谷歌浏览器" 然后按字符比较每个字符: if (input[0] == str[0])

    但是您已经在库中为您提供了一些功能(我认为是 strcmp)。你不能这样做: if (intput[0] == 'r' || 't' || 'v') 你需要做: if (intput[0] == 'r' || input[0] == 't' || input[0] == 'v')

    这是一个全球性的想法,可能有一些错误,我已经有一段时间没有接触C了。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多