[英]How to string concat and calling System with the char var
im trying to concatsomm strings and call a expext script on my server, but im a .net programer and new to c and pointers so keep messing up... what am i doing wrong here? 我试图连接字符串并在服务器上调用expext脚本,但是我是.net编程人员,并且是c和指针的新手,所以请继续弄乱……我在这里做错了什么?
or a better question wold be how should i really do this? 还是更好的问题是我应该如何真正做到这一点?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
//location of expect script that ssh and gets info on other server
char str0[] = "./home/systemio/Develop/getfile ";
char* strP = argv[1];
char str1 = (char)*strP;
char str2[] = " > file.txt";
strcat(str0,str1);
strcat(str0,str2);
printf("%s\n", str0);
system(str0);
printf("Done!!!!\n");
return 0;
}
This line will not work: 这条线不起作用:
strcat(str0,str1);
That is because str1
is not a string! 这是因为str1
不是字符串! It is a single char
. 这是一个char
。 Strings can only be char-pointers or char-arrays. 字符串只能是字符指针或字符数组。
And as noted by others, str0
is not big enough, so you will overwrite memory which will cause undefined behavior . 并且正如其他人所指出的那样, str0
不够大,因此您将覆盖内存,这将导致未定义的行为 。
If I may give an alternate solution to what you are trying to do: 如果我可以为您尝试做的事情提供替代解决方案:
char str[100];
sprintf(str, "./home/systemio/Develop/getfile %c > file.txt", argv[1][0]);
printf("%s\n", str);
system(str);
Edit: Explanation of why I use argv[1][0]
编辑:为什么我使用argv[1][0]
The reason is because of these two lines in the question: 原因是由于问题中的这两行:
char* strP = argv[1];
char str1 = (char)*strP;
These two lines get the first character from argv[1]
, in an indirect way. 这两行以间接方式从argv[1]
获取第一个字符。 If you want the whole of argv[1]
then my sprintf
will look like this instead: 如果您需要整个argv[1]
则我的sprintf
看起来像这样:
sprintf(str, "./home/systemio/Develop/getfile %s > file.txt", argv[1]);
You can allocate a buffer for the system command to ensure there is definitely enough space to construct the full command: 您可以为system命令分配一个缓冲区,以确保肯定有足够的空间来构造完整的命令:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
/* Ensure 1 argument supplied. */
if (2 == argc)
{
/* malloc()ing the system command buffer means you can safely
edit 'util' and 'output' without being concerned about the
size of an array.
The '+2' is for the char from argv[1]
and for the terminating null character. */
const char* util = "./home/systemio/Develop/getfile ";
const char* output = " > file.txt";
char* str0 = malloc(strlen(util) + strlen(output) + 2);
if (str0)
{
if (sprintf(str0, "%s%c%s", util, *argv[1], output) > 0)
{
printf("%s\n", str0);
system(str0);
printf("Done!!!!\n");
}
/* free() malloced memory. */
free(str0);
}
}
return 0;
}
Concatenation in C is not like it is done in Java or C#. 用C进行串联并不像用Java或C#进行串联。 (You can't do "A" + "B" and get "AB") (您不能执行“ A” +“ B”并获得“ AB”)
Read: http://cplusplus.com/reference/clibrary/cstring/strcat/ 阅读: http : //cplusplus.com/reference/clibrary/cstring/strcat/
strcat(dest,src) 的strcat(DEST,SRC)
You need to reserve space in destination to make sure the appended string fits inside the destination variable. 您需要在目标位置保留空间,以确保附加的字符串适合目标变量。 (must have first "A " then copy "B"). (必须首先具有“ A”,然后复制“ B”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.