string - Number of digits for an int to use in a file name in c++ -



string - Number of digits for an int to use in a file name in c++ -

this question has reply here:

print leading zeros c++ output operator (printf equivalent)? 4 answers

i need utilize integer values part of file names create outputs in c++ program. utilize code above. thing int values ( named n_irrad in code) goes 1 20000 example, need file names mc_interface00001.txt, mc_interface00002.txt, ..., mc_interface20000.txt. so, ¿how can set number of digits in file name? code i'm using mc_interface1.txt, mc_interface2.txt, ..., mc_interface20000.txt.

thanks in advance.

ofstream mc_interface; string irrad = static_cast<ostringstream*>( &(ostringstream() << n_irrad) )->str(); string mc_interface_filename = "mc_interface" + irrad + ".txt"; mc_interface.open(mc_interface_filename.c_str());

try following

#include <iostream> #include <iomanip> #include <sstream> int main() { std::ostringstream os; os << std::setw( 5 ) << std::setfill( '0' ) << 10; std::string s = "mc_interface" + os.str(); std::cout << s << std::endl; homecoming 0; }

the output is

mc_interface00010

c++ string int fstream digit

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -