Problem in VC++NET but not in VC++6 (sprintf)

Started by
2 comments, last by Drastick 21 years, 4 months ago
I just tryed working on my project with VC++NET which I had been working on with VC++6 I found that my code is not doing the same thing. I was using sprintf() like this... sprintf( buffer, TEXT("%s"), bmptxt.rollingNumberInt(100,playerunlock,4)); where rollingNumberInt() returns a char pointer (the text representation of the number I want to be in the buffer) with VC++6 this all worked So when I compiled this with VC++NET sprintf() does not put the the string of what the char pointer is into buffer, it only fills it with garbage. I took it back into VC++6 and it works fine (this was done on the same computer) whats up? has there been some change to sprintf() in .NET that I am un aware of?
Advertisement
quote:Original post by Drastick
sprintf( buffer, TEXT("%s"), bmptxt.rollingNumberInt(100,playerunlock,4));


Wide-characters ? Unicode ? Locale issues ?
Do you need wsprintf ?
Can you use std::stringstream ?
Or maybe std::wstringstream ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"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
wsprintf works thanks! It''s just strange that sprintf worked on 6 but not.NET I''ll test it later it see if it was caused by something in my project.
If you want to use sprintf then get rid of the "TEXT" macro in your second parameter. In a UNICODE build that is expanded to a L and makes the string a unicode string, but sprintf expects an ANSI (multibyte) string. In an ANSI build the TEXT macro is not expanded, so sprintf will work there.

VC++6 uses ANSI by default and that is why your code worked there, but .NET (I believe) is UNICODE by default.


P.S.
wsprintf on other hand expects a "wide" (i.e. unicode) string.

P.P.S.
Check out the "TCHAR" functions in tchar.h. basically, MS provides functions such as "_tsprintf" (I think that is right) that expand to the ANSI function (e.g. sprintf) in a ANSI build and to the wide function (e.g. wsprintf) in a UNICODE build.

Blake
Megan

This topic is closed to new replies.

Advertisement