Topic: sprintf and sscanff implimentaion using streams (check if ok)


Author: abed@ritz.cec.wustl.edu (Abed Hammond)
Date: 16 Oct 1994 10:18:36 -0500
Raw View
Follwing is two routines (string to int and int to string) that I implimented
to mimic the functionality of sprintf and sscanf.

Could some body please check for me if there are any memory leaks. The
code works fine but I am not sure about the destruction of temporaries
(for example do I need to call freeze(1) after I call the .str() member
function. )

Any comments would be great,
Thanks.

-----------------------------------------------------------------------------
#include <strstream.h>
#include <String.h>

// string to int.
int str2int(const char *s) {
   int i; istrstream(s) >> i;
   return i;
}

// int to string.
String int2str(int x) {
   char buf[32];
   ostrstream s(buf, 32); s << x << ends;
   return s.str();
}

main() {
   char s[64]; cout << "\nType an integer string: " << flush; cin >> s;
   cout << "\nThe string is: " << s
 << " The integer is: " << str2int(s) << endl;
   cout << "\nThe int is: " << str2int(s)
 << " The string is: " << int2str(str2int(s)) << endl;
}