• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

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

# Alternatives to sprintf?

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

## Recommended Posts

I remember reading something on the web that you can use cout as an alternative to sprintf. I just can''t remember where I read it. My problem is: Example: char buffer[20]; sprintf(buffer, " %d", 50); TextOut(hdc, 10, 10, buffer, strlen(buffer)); From what I remember you can do something like this: char buffer[20]; cout << 50 << buffer; TextOut(hdc, 10, 10, buffer, strlen(buffer)); But it doesn''t seem to work, can someone tell me what I am doing wrong? Jeff

##### Share on other sites
heres my little method:

  #include #include int main(){ char* someString; int someVariable = 10; ostrstream ost; // our stream obj ost << "value is " << someVariable << endl; ost.put(0); someString = ost.str(); cout << someString;}

##### Share on other sites
strstreams are deprecated. Use stringstreams instead:
  #include //etc... std::ostringstream os;int blargh = 42;//etc...os << blargh;TextOut( hdc, 10, 10, os.str().c_str() );

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

##### Share on other sites
if you still want to know what you did wrong, i think it''s supposed to be
sprintf(buffer, " %d", (double)50);
need to type cast it.

##### Share on other sites
quote:
Original post by thuned
sprintf(buffer, " %d", (double)50);

%d specifies an integer (think "D"ecimal)