C statement to C++

Started by
6 comments, last by TWild 21 years, 6 months ago
I have never had any C classes, just C++ classes. I''m following along in a book and came across this:
  
char buffer[80]; // used to hold the string

static int wm_paint_count = 0; // number of messages 

.
.
.
sprintf(buffer, "WM_PAINT called %d times", 
        ++wm_paint_count);
		
TextOut(hdc, 0, 0, buffer, strlen(buffer));

  
OK, I understand that %d appears to hold a variable (wm_paint_counter perhaps?), but am a little confused. What would be the C++ equivalent to these?
---------------------My specs (because I always forget to post them)Intel P4 2.4 GHz640 MB system memoryRadeon 9700 PROWindows XPDirectX 9DirectX 8.1 SDK
Advertisement
The string "WM_PAINT called %d times" (where %d is the value of wm_paint_count) is written to the character array buffer. So buffer[0] = ''W'', buffer[1] = ''M'', buffer[2] = ''_'', etcetera.

After that, some text output function is used to write the contents of buffer to some place (probably the screen).

I can''t think of a C++ equivalent to do this... I''ve always done it exactly like that.
The C++ equivalent would be something like:


  static int wm_paint_count = 0;std::strstream strm;strm << "WM_PAINT called " << ++wm_paint_count << " times";std::string result = strm.str ();TextOut (hdc, 0, 0, result.c_str (), result.size ());  


Personally, I prefer the C version.

Kippesoep
quote:Original post by Kippesoep
std::strstream strm;

Ugh...


  #include <sstream>static int wm_paint_count = 0;std::ostringstream strm;strm << "WM_PAINT called " << ++wm_paint_count << " times";std::string result = strm.str ();TextOut (hdc, 0, 0, result.str().c_str (), result.size ());     

quote:
Personally, I prefer the C version.

And the potential buffer overruns that come with it?


The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
try this:

        #include <sstream>    using namespace std;    ostringstream buffer; // used to hold the string    static int wm_paint_count = 0; // number of messages ...    buffer << "WM_PAINT called " << wm_paint_count << " times.";    TextOut(hdc, 0, 0, buffer.str().c_str(), buffer.str().size());     

or take a look at this.

[edited by - baumep on October 1, 2002 2:22:22 PM man am I slow ;-)]

[edited by - baumep on October 1, 2002 2:23:28 PM]
baumep
Excellent! thanks everyone
---------------------My specs (because I always forget to post them)Intel P4 2.4 GHz640 MB system memoryRadeon 9700 PROWindows XPDirectX 9DirectX 8.1 SDK
quote:Original post by TWild
OK, I understand that %d appears to hold a variable (wm_paint_counter perhaps?), but am a little confused.


The %d is what is known as a "format specifier" - in this case that is the format specifier for a signed integer. As a part of their operation sprintf (and friends) will insert the value stored in the integer at the location in the string where the %d is found. More specifically - the % sign says insert something here - the ''d'' says the expected something should be an integer. Other functions that use the same format specifier scheme are printf/wprintf/fprintf/vsprintf and a few others. To prevent buffer overruns there are ''n'' versions of most of these functions available too. The ''n'' versions require an additional parameter that specifies the maximum size of the buffer. Of course now that you have a variety of C++ examples from which to choose you won''t have to fuss much with format specifiers and the like - but you''ll probably come across them again in the future and so next time you won''t find them perplexing. Incidentally - some of the MS functions that use format specifiers do not work with floats and so you find sprintf used to fill a buffer that is then output using TextOut to be a rather common combination.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
quote:
And the potential buffer overruns that come with it?


Yep. Especially when using number variables, the number of output characters is quite predictable, so the overrun can easily be avoided. I learnt the C++ way before the C way, but I think the latter far more readable. It''s a matter of personal preference more than anything else, though.

BTW, I could never get ostringstream to work properly. It always adds garbage characters if I dump less than 5 characters into it. strstream doesn''t have this problem (at least, the implementation that comes with my compiler).

Kippesoep

This topic is closed to new replies.

Advertisement