首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost格式的字符串截断

Boost格式的字符串截断
EN

Stack Overflow用户
提问于 2016-05-20 23:07:39
回答 1查看 92关注 0票数 0
  • 我有一个用于打印当前日期的代码。但是,当我打印它时,字符串被截断了,我不知道为什么?
代码语言:javascript
复制
- So as opposed to getting "**20-5 22-1-23**" I get "**20-5 22**" only. 

我想有一些截断发生了,但我不知道为什么。

代码语言:javascript
复制
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());
EN

回答 1

Stack Overflow用户

发布于 2016-05-20 23:16:23

斯普林特有以下签名:

代码语言:javascript
复制
int snprintf ( char * s, size_t n, const char * format, ... );

其中n是Maximum number of bytes to be used in the buffer.,但您提供:

代码语言:javascript
复制
sizeof(result.c_str())

它的大小在32位架构上有4字节,在4位上有8字节。您应该在这里提供缓冲区的大小:ret

这也是为什么在"20-5 22"中有7个字符,8个字符是\0的原因

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37357114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档