Alternatives to sprintf?
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
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;}
strstreams are deprecated. Use stringstreams instead:
"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
#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
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.
sprintf(buffer, " %d", (double)50);
need to type cast it.
quote:Original post by thuned
sprintf(buffer, " %d", (double)50);
%d specifies an integer (think "D"ecimal)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement