Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » stringstream leading characters with fill and width

This thread is locked; no one can reply to it. rss feed Print
stringstream leading characters with fill and width
David Sopala
Member #5,056
September 2004

Ok what I need to get is a file name that is in the format of:
001
002
003
...
010
I need those leading 0's

Neil said it was possible to use stringstream.

my code is:

xx is a for loop variable it just increments starting at 1.
I can't seem to figure out why I can't get the leading 0's to show up. Maybe someone who has used this before could shed some light on the situation?

stringstream ss;
char prev = cout.fill('0');
cout.width(3);
ss << xx;
temp = ss.str();

got it sorta working with this code

ss.width(3);
ss.fill('0');
ss << xx;
temp = ss.str();
ss.ignore(3);

although the ignore ignores the next 3 characters and not the current buffer. I did try flushing as well, but it seemed to have no effect.

This seems to be the solution as put forth by the standard:

ss.str("");

Peter Wang
Member #23
April 2000

Do I need to /duck if I suggest sprintf, snprintf?

edit: still, it doesn't hurt to learn something new.

#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

int main()
{
    stringstream ss;
    ss << setfill('0') << setw(4) << 42;
    cout << ss.str() << endl;
}

David Sopala
Member #5,056
September 2004

This works with strings, which I am using so it saves a step. sprintf would be great if your looking for c and not C++ or using char*'s and not string classes.

The only problem was that how to clear the buffer was not clearly defined from where I found it.

The proper method being:

ss.str("");

verthex
Member #11,340
September 2009
avatar

I'm not sure if this will help you but the way I converted my strings to wide chars was like this. I take ints and convert them to string, then I concatenate a large number of them to a single string and then copy them over to a wchar*. Hope it helps.

#SelectExpand
1std::string sX, sY, sZ; 2 std::string comma = ","; 3 std::string Rp = ")"; 4 std::string Lp = "("; 5 std::string coordinates; 6 int ix, iy, iz; 7 ix = 0; 8 iy = 0; 9 iz = 0; 10 11 std::stringstream convert; // stringstream used for the conversion 12 convert <<ix;//add the value of Number to the characters in the stream 13 sX = convert.str();//set Result to the content of the stream 14 convert.str(""); 15 16 convert <<iy;//add the value of Number to the characters in the stream 17 sY = convert.str();//set Result to the content of the stream 18 convert.str(""); 19 20 convert <<iz;//add the value of Number to the characters in the stream 21 sZ = convert.str();//set Result to the content of the stream 22 convert.str(""); 23 24 coordinates = Lp + sX + comma + sY + comma + sZ + Rp; 25 26 wchar_t *p=new wchar_t[coordinates.size()]; 27 28 for(std::string::size_type i=0; i<coordinates.size(); ++i) 29 p[i]=coordinates[i];

EDIT

What you could do is take your integer and convert that to a string and then use the size() method to find the number of single integers in your first integer. I did the below code in the a.cc text input without compiling it so it might not work.

#SelectExpand
1int Inumber = 100;//convert this string 2string Snumber; 3std::stringstream convert; 4 5convert<<Inumber; 6Snumber = convert.str(); 7//find the size 8int size = Snumber.size(); 9//now take the size and subtract it from the number of max leading zeros 10int leading_zeros = 3 - size; 11//now concatenate the leading_zero count to the Snumber string. 12string leading_string; 13 14for (int count = 0 ; count < leading_zeros; count++) 15 leading_string += "0"; 16 17leading_string = leading_string + Snumber;

LennyLen
Member #5,313
December 2004
avatar

verthex said:

I did the below code in the a.cc text input without compiling it so it might not work.

It doesn't really matter whether that way would work or not as it's completely the wrong way to do it. The way Peter demonstrated is the best way to do it in C++.

verthex
Member #11,340
September 2009
avatar

LennyLen said:

It doesn't really matter whether that way would work or not as it's completely the wrong way to do it. The way Peter demonstrated is the best way to do it in C++.

Oh, I didn't know he solved it. I wasn't sure what Adam meant after he posted the answer.

Peter Wang
Member #23
April 2000

verthex said:

Adam

Where do you get this stuff?

David Sopala
Member #5,056
September 2004

Who is Adam? Now I am confused.

verthex
Member #11,340
September 2009
avatar

Who is Adam? Now I am confused.

I mixed up your name sorry. I didn't think anyone would care or notice.

Specter Phoenix
Member #1,425
July 2001
avatar

Huh?! David....Adam.....yep completely easy to mix them up ;) :P.

AMCerasoli
Member #11,955
May 2010
avatar

Shut up, Carl.

Specter Phoenix
Member #1,425
July 2001
avatar

Alright CMA Lisa... Oh f*ck, You're the Country Music Awards, hell has come to A.cc >:(.

Go to: