我可以解决这个问题吗?
int filePathSize = path.size() + s.size() + 1;
char filePath[filePathSize];
snprintf(filePath,filePathSize,"%s%s\0",path.c_str(),s.c_str());
提前感谢...
警告有一个充分的理由:
\0
标志着字符串的末端。如果您实际上需要打印零,则不能将其直接嵌入字符串中。出于这个原因,C字符串不能包含空字符。这是最明显的解决方法:
snprintf(filePath,filePathSize,"%s%s%c",path.c_str(),s.c_str(),'\0');
不需要手动添加零终端,因为snprintf
总是自动包含\0
(请参阅manpage)。