Public Group

#### Archived

This topic is now archived and is closed to further replies.

# C statement to C++

This topic is 5717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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)

##### Share on other sites
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]

##### Share on other sites
Excellent! thanks everyone

##### Share on other sites
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.

##### Share on other sites
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).

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631073
• Total Posts
2997754
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!