You're here: Snippet Directory » C/C++ (495)
Language:

itoa function

Language: English
Programming Language: C++
Published by: ergeorge [not registered]
Last Update: 5/15/2006
Views: 128


Description

This function takes an integer value and returns a null-terminated C-style string. A wrapper function returns a C++ string. The full range of positive & negative integers is supported and testable via the included test program.I initially wrote this for appending run-numbers to output data file names.

Code

1 #include <strstrea.h> 2 #include <fstream.h> 3 #include <string.h> 4 5 int main(int, char**) 6 { 7 int i_n=10; /* could be an input from anywhere */ 8 9 ostrstream o_str; 10 11 o_str << i_n; 12 o_str << ends; /* to put null at the end of buffer */ 13 14 char* c_buf=o_str.str(); /*get the buffer */ 15 16 char c_fname[80]; /* could as well be dynamic allocation */ 17 strcpy(c_fname,"xyz"); /* could as well be obtained from anywhere*/ strcat(c_fname,c_buf); /* the addition of the integer or numeric part*/ 18 strcat(c_fname,".txt"); /* the complete filename */ 19 20 ofstream o_fstr(c_fname); /*create the output file */ 21 22 o_fstr << "Done!" << endl; /* write to it -not necessary*/ 23 delete o_fstr; /* Important - delete the memory allocated while obtaining buffer*/ 24 25 return 0; 26 } 27

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS