網頁

2014年5月27日 星期二

C / C++ format string

Last Update: 2014/05/27 19:00+08
Type: Note



C sprintf - 使用時間當做範例
#include <time.h>

time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);

sprintf(fn, "t%02d%02d%02d", p->tm_hour, p->tm_min, p->tm_sec);


C++ ostringstream
#include <string>
#include <sstream>

int no = 12;
std::ostringstream oss;
oss << "data_" << no << ".text";
std::string str = oss.str();

printf("%s\n", str.c_str());