mercoledì 8 giugno 2016

String with padded number in c++

How to use a padded number in a string in c++??

ok, I need to use it to number a image sequence.

Here the include statements
#include <iomanip> // for setfill etc
#include <string>     // for string
#include <sstream>  // for stringstream

Some variables:

std::stringstream sStream;
std::string m_frameName;
int m_frameNumber;

Finally the key piece of code

sStream << std::setfill('0') << std::setw(6) << m_frameNumber;sStream >> m_frameName;

std::cout << "=========" << std::endl << "Frame no. " << m_frameNumber <<  std::endl;
std::cout << "[DBG] " << "m_frameName: " << m_frameName << std::endl;