我想通過命令行參數,然後適當連接生成shell命令,以便我可以使用system()運行它們(我知道這不是可取的,有更好的方法,但我被問到只用這種方式做)。但是在串接字符串時出現了一些問題,我通過了 這裏是代碼(我已經在每一步打印了所有內容以獲得清晰的理解,並且我還沒有編寫system()調用,首先我需要對此進行排序連接出):strcat正常工作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char* path=argv[1];
char* oldName=argv[2];
char* newName=argv[3];
char* command1="cd ";
char* command2="ren ";
printf("\n\n%s\n%s\n%s\n%s\n%s\n",command1,command2,path,oldName,newName);
strcat(command1,path);
printf("\n\n%s\n%s\n%s\n%s\n%s\n",command1,command2,path,oldName,newName);
strcat(oldName," ");
strcat(oldname,newName);
printf("\n\n%s\n%s\n%s\n%s\n%s\n",command1,command2,path,oldName,newName);
strcat(command2,oldName);
printf("\n\n%s\n%s\n%s\n%s\n%s\n",command1,command2,path,oldName,newName);
return 0;
}
但是,在將command1連接到路徑之後,所有事情都會變得混亂起來。
參見[修改-字串文本](http://stackoverflow.com/questions/ 5464183 /修改字符串文字) – amdixon
擺脫所有這些垃圾並使用'snprintf' –
到目前爲止,您在C中學習了多少字符串? – immibis