sprintf badness
blair wilson
blair_wilson at canrem.uucp
Thu Jan 24 08:02:34 AEST 1991
SC>Hi,
SC> I am running into problems when I mix floats and integers in sprintf.
SC> 1. How it is handled normally ? [Do not use "f" in sprintf ?
SC> multiply by 10 and divide by 10 ?]
SC>main ()
SC>{
SC> char buf[90];
SC> int i,j;
SC> double k;
SC> sprintf(buf,"%.4d%07.2f%.4d",45,445.34,1234);
SC> printf("buf = %s\n",buf);
SC> sscanf(buf,"%4d%7f%4d",&i,&k,&j);
SC> printf("i = %d j = %d k= %7.2f",i,j,k);
SC>}
SC>------------ output of the above program ----------------
SC>buf = 00450445.341234
111122222223333
1111 field 1 - this looks correct, your specifier %.4d, asks
for a field MINIMUM 4 wide (I would think spaces would be used
as place holders, but this may be implementation defined)
2222222 field 2 - also correct. A float 7 wide with leading
0's and 2 decimal places (this time you asked for leading 0's
with the 0 in the format spec.
3333 field 3 - also correct. A four digit integer occupying 4
spaces
You may want to seperate your fields with a space to make them
easier to decode, i.e. "\n%.4d %07.2f %.4d" will force a
couple of spaces between each field.
SC>i = 45 j = 1234 k= 8840025108807417000.00
field k messes up in this case because you need to specify %lf
for doubles, so its stuffing a float, rather than a double, at
the address you specify.
Hope this helps
email - blair.wilson at canremote.uucp
---
~ DeLuxe}ab #280sa ~ I tried the rest but bought the best!!!!
--
Canada Remote Systems. Toronto, Ontario
NorthAmeriNet Host
More information about the Comp.lang.c
mailing list