Advertisement Jump to content
Sign in to follow this  

stdio snprintf

This topic is 4683 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

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<T> today but my vc80 compiler didn’t like
numeric_limits<float> fi;

It’s confused and wants to use the max macro instead. #define Max(a,b)….

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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 );

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

character count = 71

>For second question you need <limits.h>

Share this post

Link to post
Share on other sites
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 <windows.h> or as a global project define. Also numeric_limits contains only static members, so you want numeric_limits< float >::max(). And the header is <limits>, not <limits.h>.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!