变量类型的系统wget

发布于 2024-11-08 20:19:28 字数 232 浏览 5 评论 0原文

所以,我有几个 url 存储在一个名为 url 的字符指针数组中,

我想在每个 url 上调用 wget,但我不断收到以下错误。

invalid operands to binary + (have 'char *' and 'char *')

我的程序是C语言的

system("wget" + url[0]);

So, I have several url's stored in a char pointer array called url

I want to call wget on each url, but I keep getting the following error.

invalid operands to binary + (have 'char *' and 'char *')

My program is in C

system("wget" + url[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2024-11-15 20:19:28

在 C 中,+ 运算符不适用于字符串。要连接两个字符串并将结果传递给 system(),您可以执行以下操作:

char buffer[ENOUGH_SPACE_TO_HOLD_CONCATENATED_RESULT];  /* Destination buffer for our command */
snprintf(buffer, sizeof(buffer), "wget %s", url[0]);    /* You can also use strcat and friends for this step */
system(buffer);                                         /* Now execute it */

In C, the + operator does not work on strings. To concatenate two strings and pass the result to system() you can do the following:

char buffer[ENOUGH_SPACE_TO_HOLD_CONCATENATED_RESULT];  /* Destination buffer for our command */
snprintf(buffer, sizeof(buffer), "wget %s", url[0]);    /* You can also use strcat and friends for this step */
system(buffer);                                         /* Now execute it */
空名 2024-11-15 20:19:28

在 C 中,不能使用 + 运算符连接两个字符串。使用 strncat 代替:

NAME
       strcat, strncat - concatenate two strings

SYNOPSIS
       #include <string.h>

       char *strcat(char *dest, const char *src);

       char *strncat(char *dest, const char *src, size_t n);

DESCRIPTION
       The  strcat() function appends the src string to the dest string, over‐
       writing the null byte ('\0') at the end of dest, and then adds a termi‐
       nating  null  byte.   The  strings may not overlap, and the dest string
       must have enough space for the result.

       The strncat() function is similar, except that

       *  it will use at most n characters from src; and

       *  src does not need to be null-terminated if it  contains  n  or  more
          characters.

       As  with  strcat(),  the resulting string in dest is always null-termi‐
       nated.

       If src contains n or more characters, strncat() writes  n+1  characters
       to  dest  (n  from src plus the terminating null byte).  Therefore, the
       size of dest must be at least strlen(dest)+n+1.

谷歌搜索“strncat”将为您提供大量有关如何使用它的示例。

In C, you can't concatenate two strings using the + operator. Use strncat instead:

NAME
       strcat, strncat - concatenate two strings

SYNOPSIS
       #include <string.h>

       char *strcat(char *dest, const char *src);

       char *strncat(char *dest, const char *src, size_t n);

DESCRIPTION
       The  strcat() function appends the src string to the dest string, over‐
       writing the null byte ('\0') at the end of dest, and then adds a termi‐
       nating  null  byte.   The  strings may not overlap, and the dest string
       must have enough space for the result.

       The strncat() function is similar, except that

       *  it will use at most n characters from src; and

       *  src does not need to be null-terminated if it  contains  n  or  more
          characters.

       As  with  strcat(),  the resulting string in dest is always null-termi‐
       nated.

       If src contains n or more characters, strncat() writes  n+1  characters
       to  dest  (n  from src plus the terminating null byte).  Therefore, the
       size of dest must be at least strlen(dest)+n+1.

Googling "strncat" will give you plenty of examples of how it's used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文