sprintf corrupted output using string as argument

This is a discussion on sprintf corrupted output using string as argument within the C++ Programming forums, part of the General Programming Boards category; Hi everyone,
I have searched high and low for the answer to what I am sure is a simple mistake ...

sprintf corrupted output using string as argument

Hi everyone,

I have searched high and low for the answer to what I am sure is a simple mistake that I just can't put my finger on!

When using the %s argument with sprintf to pass a string of characters, the resulting output is unexpected and the rest of the argument calls are also corrupted. I have tried to specify the precision using something like %.4s to read four characters but the same thing happens.

sprintf is a function from C standard library and it works with C-style strings. You should give up C style functions and use C++ equivalents (std::cout, std::cin and others), which are less error-prone and provide better compile-time type checking. But to fix this one, use c_str() method:

Unfortunately the main part of the code generates objects on-the-fly so I can't do this

Originally Posted by rags_to_riches

A better option would be to use a strings and a stringstream, instead of dragging C char arrays and I/O functions like sprintf into the picture

Thanks! Stringstream class objects seem to be a lot easier to manage for what I'm trying to do, as a lot of values from other variables end up pulled into my statement string.

I did hit a little snag, the statement is eventually passed as an SQL query through ODBC, and the function for doing this, SQLExecDirect(), expects a SQLCHAR* for the statement input. It didn't like using &ss.str() I worked around it by copying the stringstream output to a string as below and used string.c_str(), but is there a tidier way to use the stringstream object directly?

A third approach might be Boost.Format. It's even more extensive than sprintf and family on formatting.
Also, don't cast to SQLCHAR*. If you've done it right, there will be no need for casting. If it doesn't work, then you did something wrong (I can't imagine a cast being required).

Originally Posted by http://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx

The other main difference between sprintf_s and sprintf is that sprintf_s takes a length parameter specifying the size of the output buffer in characters. If the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked. Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated (unless the buffer size is zero).

This argument is the second one and looks something like:

Code:

char buff[100];
...
sprintf_s(buff,sizeof(buff),"Hello %s, how are you today?",my_name);

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens