Re: Faster way to write in a file
- From: Morris Dovey <mrdovey@xxxxxxxx>
- Date: Wed, 13 Feb 2008 15:39:20 -0500
LilacSkin wrote:
#include <stdio.h>
void convert(long long v,char *s,int sz,int dp)
{ char *p = s + sz;
long long x = 1LL;
int sign = v < 0.0;
if (sign) v = -v;
while (dp--) x *= 10;
x = x * v;
*p-- = '\0';
do
{ *p-- = '0' + (x % 10);
} while ((p >= s) && (x /= 10));
while (p >= s) *p-- = '0';
if (sign) *s = '-';
}
Pretty close. Since you're not working with a double, you can
dispense with the fractional part logic and shorten to something
like:
void convert(long long x,char *s,int sz)
{ char *p = s + sz;
int sign = x < 0;
if (sign) x = -x;
*p-- = '\0';
do
{ *p-- = '0' + (x % 10);
} while ((p >= s) && (x /= 10));
while (p >= s) *p-- = ' ';
if (sign) *s = '-';
}
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
.
- References:
- Faster way to write in a file
- From: LilacSkin
- Re: Faster way to write in a file
- From: santosh
- Re: Faster way to write in a file
- From: Mark Bluemel
- Re: Faster way to write in a file
- From: Morris Dovey
- Re: Faster way to write in a file
- From: LilacSkin
- Re: Faster way to write in a file
- From: Morris Dovey
- Re: Faster way to write in a file
- From: LilacSkin
- Faster way to write in a file
- Prev by Date: Re: OK to disguise a macro function as a function?
- Next by Date: Re: Dereference an array pointer... UB?
- Previous by thread: Re: Faster way to write in a file
- Next by thread: Re: Faster way to write in a file
- Index(es):
Relevant Pages
|