1753884 Members
7347 Online
108809 Solutions
New Discussion юеВ

problem with vsprintf

 
SOLVED
Go to solution
rajanandhini
Advisor

problem with vsprintf

Hi,

When I try to compile the following code, I get an additional message from the buffer.

The code that we use has,
buf[0] = '\0';
va_start(va, fmt);
vsprintf(buf, fmt, va);
va_end(va);

len = fprintf(work,"%s",buf);

The buffer should only contain the content of fmt. But, it also contains the text "parent information" along with the content of fmt. i.e, if the value of fmt is "hi", the value of buf should also be "hi". But the actual value that we get is, "parent informationhi".

Please help.

Thanks,

nan
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: problem with vsprintf

buf[0] = '\0';

You don't need this. The following seems fine:
va_start(va, fmt);
vsprintf(buf, fmt, va);
va_end(va);

>The buffer should only contain the content of fmt. But, it also contains the text "parent information" along with the content of fmt.

It shouldn't have this. What is in fmt?

len = fprintf(work,"%s",buf);

It is silly to use fprintf(%s), you should call fputs or fwrite. Or you could have called vfprintf to start with.
rajanandhini
Advisor

Re: problem with vsprintf

fmt is a character pointer and it contains the string, "hi am fine"
Dennis Handly
Acclaimed Contributor
Solution

Re: problem with vsprintf

>fmt is a character pointer and it contains the string, "hi am fine"

What you are saying can't happen unless you have your own bogus version of vsprintf.

If you set a breakpoint on vsprintf, are you in libc when you hit it? What does "bt" give?
Dennis Handly
Acclaimed Contributor

Re: problem with vsprintf

Or you can use "info func vsprintf" to see how many you have.
Peter Nikitka
Honored Contributor

Re: problem with vsprintf

Hi,

how is the variable buf declared?
Does 'buf' point to an allocated chunk of memory big enough to hold the varargs?
Best use vsnprintf() in that cases because not buffer overrun can occure when correctly used.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"