首页 > 编程语言 > 详细

c++ int 转 string 实现前缀补0

时间:2017-11-09 19:04:28      阅读:782      评论:0      收藏:0      [点我收藏+]

最近需要读取一些格式化路径的文件。路径文件名字最后是数字从0开始到100结束,但是占了4位,即0000到0100这样。

需要将int转成string的同时加上前缀0.

方法一:

用sprintf()函数。

char pointscloud[128];
sprintf(pointscloud, "/model%d/action%d/%04dpointscloud.txt", model, action, fileIndex);

方法二:

使用c++的stringstream 。

添加头文件

#include <iostream>
#include <iomanip>

stringstream ss;
    
ss << "/body3DCoordinates" << i << "_" << setw(4) << setfill(0) << j << ".txt";
cout << ss.str() << endl;

 

参考:

https://zhidao.baidu.com/question/284142736.html

http://bbs.csdn.net/topics/10129157

c++ int 转 string 实现前缀补0

原文:http://www.cnblogs.com/hellowooorld/p/7810849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!