Skip to main content
gamedev.net
🔒 Locked

stdio snprintf

Started by Leadorn Mar 30, 2006 at 5:38 AM 3 replies 900+ views
Original Post
Leadorn
Leadorn
Hello I want to write buffer safe code so I need to use sprintf with a buffer length as input so that the function doesn’t write past the end of my buffer. I know there is a new lib available but where can I find them. Next question: I was looking at numeric_limits today but my vc80 compiler didn’t like
[SOURCE]
numeric_limits<float> fi;
fi.max();

[/SOURCE]
It’s confused and wants to use the max macro instead. #define Max(a,b)….
Leadorn
Leadorn
One more question

If I need to print floats and integers in my std::string what is the most common and safe way to do that?
TEUTON
TEUTON
The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format.

Example: Taken By MSDN(just removed void part)
#include <stdio.h>int main( void ){char  buffer[200], s[] = “computer", c = ‘l’;int   i = 35, j;float fp = 1.7320534f;/* Format and print various data: */j  = sprintf( buffer,     “\tString:    %s\n", s );j += sprintf( buffer + j, “\tCharacter: %c\n", c );   j += sprintf( buffer + j, “\tInteger:   %d\n", i );   j += sprintf( buffer + j, “\tReal:      %f\n", fp );printf( “Output:\n%s\ncharacter count = %d\n", buffer, j );}


Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

character count = 71



>For second question you need
Enigma
Enigma
You're using C++. Use it.
std::string buffer;std::ostringstream writer;writer << "This is some of my text " << 32.0f << a_variable;buffer = writer.str();

If you want easier format control look into Boost.Format.

With limits your problem is most likely the stupid min and max macros in windows.h. Add a #define NOMINMAX either before including or as a global project define. Also numeric_limits contains only static members, so you want numeric_limits< float >::max(). And the header is , not .

Σnigma
Leadorn
Leadorn
okej ill use the std::ostringstream


thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.