How do you format integers to two digits? 2 -> 02

I'm wanting to open a file using date and time but I want my date formatted this way: 08/08/2012 as opposed to: 8/8/2012

I know functions like printf and CSLog that can output integers that way, like so:

1
2
3
4
5
    time_t secs=time(0);
    tm *t=localtime(&secs);
    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
           t->tm_year+1900,       t->tm_mon+1,           t->tm_mday,
           t->tm_hour,       t->tm_min,        t->tm_sec);


but I would like what this^^ outputs to be put into a string so I can

ofstream fout(date.c_str());

Any help would be greatly appreciated.
Use sprintf() if you want to output to a string instead of standard output, like printf() does.
Ahhh, either of those should work. Thanks a lot.
cout<<setw(2)<<setfill('0');
should do.
Topic archived. No new replies allowed.