vs2005 VC++ question - %n format specified disabled

NuclearNed

Raconteur
May 18, 2001
7,870
361
126
I'm still using VS2003, but I'm helping a coworker learn VC++ w/ MFC on VS2005. It appears that the %n format specifier for CString and printf has been disabled for security reasons.

So now what is the proper way to format integers as strings?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Well here is one quick way to do it:

#include <stringstream>

wstringstream conv;
CString cstr;

int n = 1234567;

conv << n << ends;
cstr = conv.str();

Note: from memory, as I don't have a compiler on this machine. You might have to do:

cstr = conv.str().c_str() depending on what conversion methods a CString supports.

There are probably a dozen other ways to do this, as strings in Windows/C++/STL/ATL/MFC is probably the most farked up area of system development, but that is the one I currently use.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Use old printf standby formatting codes.

%d
%ld
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
ostringstream defined in iostream in the "std" namespace will do what you want