-
char + int = ?
Ok lets think about that: You have a char x, and you want to abb a int y to the end. This is usefull, if you have many files, and instead of giving them names i want them just to have numbers. I am thinkink about something liek the strcat funktion or something like that.
Thankx to all of you.
-tare
-
If I've understood what you want to do, here's two methods -
Code:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
char text[]="abcdefg";
int num =10;
//Option 1
char chars1[30];
ostringstream os;
os << text << num;
strcpy(chars1, os.str().c_str());
cout << chars1 << endl;
//Option 2
char chars2[30];
sprintf(chars2,"%s%d",text,num);
printf("%s\n",chars2);
return 0;
}
-
ok thanks man, both options are working but i have couple questions:
ostringstream os; //What is that for?
os << text << num; //What is that doing?
strcpy(chars1, os.str().c_str()); //What is os.str().c_str() ????
-lata tare
-
My understanding is that ostringstreams are a mechanism to manipulate strings by writing directly to the string object. You create an ostringstream object just like you create a fstream or ifstream or ofstream oblject and use it to write to the string. In this case the << operator first writes the contents of the variable text to the ostringstream object os and then a second << operator writes the contents of the variable num to the string eventhough num is of type int rather than a string. This obviates the need to somehow convert an int to a string. However, once in the ostringstream object you need to get the information back out.
os.str().c_str() converts the information in the ostringstream object os first to a form that can then be transfered to the routine c_style string chars1, so you can then print the value in chars1 (what was in os) to the screen with cout.
Another way to convert from one type, say int, to a string is to use the C style function called sprintf() as opposed to using the C++ stream class ostringstream.
If you happen to have the conio.h file it has functions called itoa(), ltoa(), and (I believe) ftoa() that can do this conversion for you as well. The problem is that these functions are not standard C++ functions and may not port to other compilers, and conio.h is not ANSI C++ standard and therefore not included in all compilers either. On the other hand sprintf() and ostringstreams are ANSI standard and should port to any compiler that is in compliance with ANSI standards.
-
os.str().c_str(), obtains a string object from the stringstream, os.str() will return a string object, this string object is then converted to constant C style string. This is shorthand for doing -
string temp = os.str();
strcpy(chars1,temp.c_str());
For doing simple things like this sprintf() would probably be the best (but less type safe) option as it is more straightforward, and doesn't need the several several conversion steps.
A futher method would be to use a strstream instead of a stringstream. A char* is obtained directly from a strstream, but I believe they've been depreciated in the C++ standard, so for future portability they're probably not a good option.
Also, even if _itoa() is not supported by your compiler, the function is fairly simple so you could always implement your own if needed.
-
Thanks unregistered stranger!!!
now i have a new problem:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
i need also
#include <istream.h>
#include <ostream.h>
#include <streamb.h>
#include <strstrea.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
But than i get an error message...
Why can't i use some files with .h and some without????
What is the .h for????
Thanks again.
tare
-
In some cases the old style headers are incompatible with the new. In each case these are what you should be including (in the std namespace).
#include <istream.h> - <fstream>
#include <ostream.h> - <fstream>
#include <streamb.h> - <streambuf>
#include <strstrea.h> - <strstream>
#include <iostream.h> -<iostream>
#include <fstream.h> - <fstream>
#include <stdlib.h> -<cstdlib>
#include <string.h>- <cstring>
#include <stdio.h>- <cstudio>
#include <time.h> - <ctime>
-
tare
are the new style headers different than the old ones?
-
Can anyone give me an example how to add an number to the end of a char with the itoa() function? I tried it but i cant figure it out...
thankx