Padding zeros, for a filename for example

Hey All,

I just found a useful C++ function that allows you to pad integers for filenames or other uses.

  
  
int filenumber;  
std::ostringstream localOSS;  
string fileName;   
  
filenumber = 2034;  
  
localOSS << "prefix_" << setw(10) << setfill('0') << fileNumber << ".jpg";  
  
fileName = localOSS.str();  
  

Fills up the string with zeros!

Cheers,

JGL

or use printf:

  
  
// for leading 0's  
printf("%04i\n", number);  
// or for leading spaces (nice for table formatting)  
printf("% 4i\n", number);  
  

these also work with sprintf()

I know this is old, but for my own reference.

cout<<ofToString(i,4,'0');