Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A useful side benefit of sprintf...

Status
Not open for further replies.

bodhi

Programmer
Jan 3, 2002
67
GB
This may be common knowledge but I only came across it the other day.

The fact that sprintf, etc.. return the number of bytes can be fairly useful particularly in things like string parsing
to keep track of your position.

For example..

VOID MYCLASS::stringdecode (INT *b)
{ CHAR *iptr,*optr;
CHAR tmp[MAX_STRING_SIZE];
optr = tmp;

switch(*b)
{ default :iptr = lookup[foo].decodestr;
blah..blah..
}

blah...blah..
while (iptr != NULL)
{ if (*iptr++ == '%')
{ decode_y (iptr,&optr);
etc...
}
else
*optr++ = *iptr++;
}
}

VOID MYCLASS::decode_y (INT *data, CHAR **dst)
{ blah..blah..
if (test)
{ *dst += sprintf(*dst,"%s",mystring; }
else
{ *dst += sprintf(*dst,"$%02X,Y",*(data+1)); }
}

I have used this surprisingly often since a colleague pointed it out.

Hopefully it is not the first thing they teach in college these days or some such :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top