我的代码中有很多 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");
最佳答案
您可以将格式字符串与 snprintf()
结合使用(与 sprintf()
相比安全):
snprintf(result, 10000,
"// Assign new material to worldGlobe\nshadingNode -asShader lambert -n %s;\n",
material);
关于c - C中字符串连接的代码太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647307/