Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using sprintf() with 0's

Reply
Thread Tools

using sprintf() with 0's

 
 
markww
Guest
Posts: n/a
 
      08-16-2006
Hi,

Is there a way to use sprintf() to put a number of zeros before a
number, like:

sprintf(szBuffer, "%i", 7);

"007";

I have a list of numbers I want to sort that are text based, and I want
to put zeros before some so they get sorted in order like:

007
008
009
010
011
012

Thanks

 
Reply With Quote
 
 
 
 
billy
Guest
Posts: n/a
 
      08-16-2006
How about sprintf (szBuffer, "%03i", 7)?

The leading 0 of "%03i" means to print 0 in front of the number; 3
means the field width is 3.

markww wrote:
> Hi,
>
> Is there a way to use sprintf() to put a number of zeros before a
> number, like:
>
> sprintf(szBuffer, "%i", 7);
>
> "007";
>
> I have a list of numbers I want to sort that are text based, and I want
> to put zeros before some so they get sorted in order like:
>
> 007
> 008
> 009
> 010
> 011
> 012
>
> Thanks


 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      08-16-2006

markww wrote:
> Hi,
>
> Is there a way to use sprintf() to put a number of zeros before a
> number, like:
>
> sprintf(szBuffer, "%i", 7);
>
> "007";
>
> I have a list of numbers I want to sort that are text based, and I want
> to put zeros before some so they get sorted in order like:
>
> 007
> 008
> 009
> 010
> 011
> 012
>
> Thanks


this padds it with zeros, and lenghth 3:

sprintf(szBuffer, "%03i", 7);

 
Reply With Quote
 
markww
Guest
Posts: n/a
 
      08-16-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> markww wrote:
> > Hi,
> >
> > Is there a way to use sprintf() to put a number of zeros before a
> > number, like:
> >
> > sprintf(szBuffer, "%i", 7);
> >
> > "007";
> >
> > I have a list of numbers I want to sort that are text based, and I want
> > to put zeros before some so they get sorted in order like:
> >
> > 007
> > 008
> > 009
> > 010
> > 011
> > 012
> >
> > Thanks

>
> this padds it with zeros, and lenghth 3:
>
> sprintf(szBuffer, "%03i", 7);


That is some hot gravy, thanks guys.

Mark

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-17-2006
"markww" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> Is there a way to use sprintf() to put a number of zeros before a
> number, like:
>
> sprintf(szBuffer, "%i", 7);
>
> "007";
>
> I have a list of numbers I want to sort that are text based, and I want
> to put zeros before some so they get sorted in order like:
>
> 007
> 008
> 009
> 010
> 011
> 012
>
> Thanks


You could do as others said, but I would probably just use stringstring to
convert it to an int and sort that anyway. Either way you're going to need
a temporary to sort on (in your case szBuffer, in my case an int). This
also has the advantage of being able to sort arbitrary sized numbers. What
happens when you get to 1000?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-18-2006
Jim Langston wrote:
> [..] I would probably just use
> stringstring to [..]


"stringstring"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-18-2006
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> Jim Langston wrote:
>> [..] I would probably just use
>> stringstring to [..]

>
> "stringstring"?
>
> V


You know how many times I type std::stringstring in a program and get a
syntax error? Just about every time I use it actually.

Of course I meant std::stringstream


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-18-2006
markww wrote:
> (E-Mail Removed) wrote:
> > markww wrote:
> > > Is there a way to use sprintf() to put a number of zeros before a
> > > number,

> >
> > sprintf(szBuffer, "%03i", 7);

>
> That is some hot gravy, thanks guys.


Bear in mind that the 3 doesn't limit the field width, eg:

sprintf(buf, "%03i", 1234);

will still write "1234". So buf needs to be big enough for the
biggest possible integer (including 0 terminator).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments