- So as opposed to getting "**20-5 22-1-23**" I get "**20-5 22**" only.
我想有一些截断发生了,但我不知道为什么。
using namespace std;
const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
const boost::format f = boost::format("%d-%d %ld-%ld-%ld")
% now.date().year_month_day().day.as_number()
% now.date().year_month_day().month.as_number()
//% now.date().year_month_day().year.as_number()
% now.time_of_day().hours()
% now.time_of_day().minutes()
% now.time_of_day().seconds();
const string result = f.str();
snprintf(ret, sizeof(result.c_str()), "%s", result.c_str());发布于 2016-05-20 23:16:23
斯普林特有以下签名:
int snprintf ( char * s, size_t n, const char * format, ... );其中n是Maximum number of bytes to be used in the buffer.,但您提供:
sizeof(result.c_str())它的大小在32位架构上有4字节,在4位上有8字节。您应该在这里提供缓冲区的大小:ret。
这也是为什么在"20-5 22"中有7个字符,8个字符是\0的原因
https://stackoverflow.com/questions/37357114
复制相似问题