Putting Numbers into Strings
- Code for this is located in intsToString.C
- The C way
- Output in C is done with the printf family of functions.
- #include <stdio.h>
- int print ( const char * format, ... );
- printf("hello %s, you are %d years old\n", name, age);
- %d matches integers, %f float, ...
- More info
- On success, returns the number of characters printed.
- sprintf will print to a string.
- The string is an array of characters and it will terminate with the null.
- int sprintf ( char * str, const char * format, ... );
- sprintf(word, "%s is %d years old", name, age);
- It is richer than this, but this will do.
- The C++ way.
- String streams are derived from streams.
- #include <sstream>
- ostringstream foo;
- You can to such a stream in a the normal way (<<)
- the .str() method allows you to get and set the string.