How do i convert string to char array?

Well the title says it all.
Umm and yes i need to convert them cause i first count the length of a string then add to the end of the string then need to convert it to a char so i can create a file with that string content.
Heres the code.

1
2
3
4
5
6
TempNumOne=FileMeasure.size();
FileMeasure=FileMeasure+".txt";
//Filename=FileMeasure;
cout<<FileMeasure;
system("PAUSE");
ofstream NewFile(Filename);


FileMeasure is a string.
Filename is a char array.[100]
TempNumOne is a interger.

//Filename=FileMeasure;
that is the line i need to convert it to a char array.
could someone show me what that line should look like.?
Last edited on
RESOLVED!!! THIS IS FOR ANYONE WHO NEEDS TO KNOW THIS TOO!

1
2
3
4
5
6
7
8
       
string FileMeasure="Hello FILE!"
int TempNumOne=FileMeasure.size();
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
        {
            Filename[a]=FileMeasure[a];
        }
That code doesn't quite work because it does not ensure that Filename is null terminated.
I suggest looking up the str() function on string and then strcpy.
1
2
FileMeasure.append(".txt");
char *fileName = (char*)FileMeasure.c_str();
I like this one better:
1
2
3
4
//std::string s
char *a=new char[s.size()+1]
a[s.size()]=0;
memcpy(a,s.c_str(),s.size());
Thanks guys!

:)

:D

:P

XD

:]
Topic archived. No new replies allowed.