C中字符串连接的代码太多

use*_*632 1 c string

我的代码中有很多strcat行.有没有更好的方法来连接C中的字符串?

char material[50]; // it is defined before this code.
char result[10000];
strcpy(result, "// Assign new material to worldGlobe\n");
strcat(result, "shadingNode -asShader lambert -n ");
strcat(result, material);
strcat(result, ";\n");
Run Code Online (Sandbox Code Playgroud)

Tim*_*per 11

您可以将格式字符串与snprintf()(安全性比较sprintf())结合使用:

snprintf(result, 10000,
    "// Assign new material to worldGlobe\nshadingNode -asShader lambert -n %s;\n",
    material);
Run Code Online (Sandbox Code Playgroud)