I know it can be done

closed account (4Gb4jE8b)
I've even seen threads HERE about it... but guess who didn't pay attention... and who can't find it again because he doesn't know the correct terminology to search for it? ME!

Alright so a really simple issue, i have a for loop that increases a size of an integer value y on each iteration. But as this value goes into the name of a text file, and increases well beyond ten (and in some cases 100) i would like a 0 placeholder in the smaller numbers so each text file has the same number of characters in it.

The reason for this is actually do to organization of the files, but i won't get into that unless necessary.

I don't really feel like this gave a clear question so... let me show you.
1
2
3
4
for(y = 1;y<some_number;y++)
{
     //y goes into "file name"
}


outputs "file name 1" "file name 2" "file name 3" ... "file name 14"

what i want it to output "file name 01" "file name 02" "file name 03" ... "file name 14"

the file names are created using an ostringstream, if that has any effect on it. I can post my actual code, but i'm tired and it's late, so i guess i'm lazy. Let me know if you feel it's necessary to see more, and thanks for any help
Last edited on
use the setw and setfill stream manipulators.
remember to #include <iomanip>

Example:
1
2
3
4
5
6
7
    ofstream of("output.txt");

    for (int n=1; n< 100; n++)
    {
        of << std::setw(2) << std::setfill('0') << n << "  ";
    }
    of.close();
Last edited on
closed account (4Gb4jE8b)
thanks, it worked perfectly
Topic archived. No new replies allowed.