Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MonkeyInBlack

vsprintf

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m encountered a little problem when i modified an old logfile-class :
void CLogfile::println(char* tekst, ...)
{
	va_list		argumentPtr;		// This will hold the pointer to the argument list


	if (tekst == NULL)			// Check if a string was given

		return;				// In case not


	va_start(argumentPtr, tekst);

	vsprintf(tekst, tekst, argumentPtr);	// Now add the arguments into the full string


	va_end(argumentPtr);			// This resets and frees the pointer to the argument list.


}
The original used a vfprintf(logfile, tekst, argumentPtr); to print the text directly in the textfile. Unfortunatly with using vsprintf to store it in the buffer the program hangs. Can anyone tell me why (what to do about it) ?
The ever vigilant...

Share this post


Link to post
Share on other sites
Advertisement
You can''t use tekst for both the destination and the source. You have to specify different buffers for each. Otherwise, as vsprintf writes to tekst, it will overwrite itself, obliterating the source string.

Share this post


Link to post
Share on other sites
I suspected that it might have been that so i allready tried to store the text in another string but that didn''t change the outcome. Thanks though



The ever vigilant...

Share this post


Link to post
Share on other sites
quote:
Original post by MonkeyInBlack
I suspected that it might have been that so i allready tried to store the text in another string but that didn''t change the outcome.


Show us how you tried to do that.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by Pipo DeClown
Maybe.. it''s overflowing? You sure the char-array is big enough?

O yeah.. xsprintf()s don''t seem to work with char-pointers.. Try making a local char-array..



That was the problem idd. A quick test did work :


void CLogger::println(char* tekst, ...)
{
va_list argumentPtr; // This will hold the pointer to the argument list


if (tekst == NULL) // Check if a string was given

return; // Don''t render anything then


va_start(argumentPtr, tekst);

char buffer[256] = "";

vsprintf(buffer, tekst, argumentPtr); // Now add the arguments into the full string


strcat(buffer,"
\n");

print(buffer);

va_end(argumentPtr); // This resets and frees the pointer to the argument list.

}


This works offcourse only if buffer < 256 chars. I now tried to get the length of the tekst with arguments with the _vscprintf function descriped on msdn but i''m getting an undeclared identifier. Anyone got a solution for that ?


int len = vscprintf(tekst, argumentPtr);


Thanks y''all for answering !



The ever vigilant...

Share this post


Link to post
Share on other sites
Use vsnprintf() to limit the length of the output to the size of your buffer. Or switch to C++.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by MonkeyInBlack
Can''t do that - the reason is the same as for my last question : isn''t available for msvc++ 6.0


In their library, MS prefixes those function names with an underscore (presumably because they are "only" POSIX-standard functions). Try it that way (or ... time to upgrade. At the very least, I believe I''ve seen a SP6 for VC6).


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!