【问题标题】:How to use String in System() Command如何在 System() 命令中使用字符串
【发布时间】:2014-04-10 14:08:43
【问题描述】:

我想为 wget 制作一个程序,它会询问您要从哪个 URL 下载然后下载,但我不知道如何添加字符串“wget”和 url 并将其放入 system()命令。我知道添加字符串有多种可能性,但对我没有任何帮助。请你帮助我好吗? (代码应该是这样的:)

char url[32];
char wget[32];
scanf("%s", &url);
strcpy(wget, "wget");
strcat(wget, url);
system(wget);

【问题讨论】:

  • wget 后面的 空格 怎么样?我的意思是"wget "
  • @SakthiKumar 没错,它并不能解决问题。当我使用printf("%s", wget) 查看输出时,它会显示wget ��

标签: c char wget strcpy strcat


【解决方案1】:

您需要在wgeturl 之间留一个空格,因此不要使用strcat,只需使用sprintf,如下所示:

  int main ()
    {
        char url[32];
        scanf("%s", url);
        int len=strlen(url)+4+2; //4 for wget and 2 for space and safe
        char buffer[len];
        sprintf(buffer,"wget %s",url);
        system(buffer);
    }

在你的情况下,如果我输入 url www.google.com 然后 strcat 之后的最终命令变为

wgetwww.google.com

这是无效的,但应该是wget www.google.com

【讨论】:

    【解决方案2】:

    其他人比我更快地指出了缺少的空间,但实际上你的代码有更多“错误”,所以如果你原谅我,我会切换到教程模式一两分钟。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    // The maximum allowed length of an input line, including the newline.
    // Longer lines result in an error message.
    const size_t INPUTMAXSIZE = 32;
    
    int main()
    {
        // You can change the command at will. Length is dynamic.
        // Do not forget the space at the end.
        char * command = "wget ";
    
        char parameter[INPUTMAXSIZE];
        char * ptr;
    
        // Using fgets, which allows us to avoid buffer overflow.
        if ( fgets( parameter, INPUTMAXSIZE, stdin ) == NULL )
        {
            puts( "Error while reading input." );
            exit( EXIT_FAILURE );
        }
    
        // fgets stores the newline as well
        if ( ( ptr = strchr( parameter, '\n' ) ) != NULL )
        {
            // Replace newline with terminating null
            *ptr = 0;
        }
        else
        {
            // Input longer than INPUTMAXSIZE
            puts( "URL too long." );
            exit( EXIT_FAILURE );
        }
    
        // Allocating the buffer memory dynamically allows us to avoid
        // a second magic number. Re-using 'ptr'.
        if ( ( ptr = malloc( strlen( command ) + strlen( parameter ) + 1 ) ) == NULL )
        {
            puts( "Memory allocation failed." );
            exit( EXIT_FAILURE );
        }
    
        sprintf( ptr, "%s%s", command, parameter );
    
        printf( "system( \"%s\" ) returned %d.\n", ptr, system( ptr ) );
    
        free( ptr );
    
        return( EXIT_SUCCESS );
    }
    
    • 始终以完整、可编译的形式提供代码。
    • 尽可能减少“幻数”的使用。
    • 尽可能使用常量。
    • 使您的代码在面对意外/格式错误的输入时保持稳定。错误失败是可以原谅的,转储核心不是。
    • 检查您正在使用的可能会失败的函数的返回码。

    我并不是说我上面的代码是完美的,但我认为其中有一两个教训。希望对你有帮助。

    【讨论】:

      【解决方案3】:

      scanf("%s", &amp;url); 条带 &amp; & 符号,因为它不是必需的。 url 本身就是 scanf() 所需数组的基地址。

      数组基本上衰减为指针,因此不需要在数组上使用&amp; 运算符来获取指针。如果您认为自己有一个数组但实际上有一个指针,那可能会很危险。

      【讨论】:

      • 什么意思?当我删除 &amp; 时出现错误。
      • scanf() 中,格式“%s”需要char * 类型,但您传递的参数2 是char (*)[32] 类型。
      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多