Archived

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

jthdmb10

Alternatives to sprintf?

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 this post


Link to post
Share on other sites
heres my little method:

  
#include <strstream.h>
#include <iostream.h>

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 this post


Link to post
Share on other sites
strstreams are deprecated. Use stringstreams instead:
  

#include <sstream>

//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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
do you need that buffer? If you don''t things become a lot easier. Just use the output operator to push your information into the outside world. If you are worried about performance don''t worry, the streams are buffered for you.

Share this post


Link to post
Share on other sites