How do I convert a C++ string to a char array?

It seems that it is not as easy as I have thought...

How does one convert a string to a char array?

This is what I have:

1
2
3
4
5
6
char cFilm[100];
string sFilm;

GetDlgItemText(Dialog, DIALOG_EDIT_BOX_NAME, cFilm, 100); //(this works)
sFilm = cFilm; //(this works too)
cFilm = sFilm; //(this does not work) 


Thank you!
Last edited on
Use strcpy() function:
strcpy(cFilm,sFilm.c_str());

http://cplusplus.com/reference/clibrary/cstring/strcpy/
Last edited on
Thank you Null, that worked perfectly as intended!

But now I stumbled upon another issue that was not correctly addressed in my first post... It appears that simply assigning sFilm = cFilm does not at all bring any of the content within cFilm into sFilm. What function would you suggest that I use to assign a char array to string?
It appears that simply assigning sFilm = cFilm does not at all bring any of the content within cFilm into sFilm.


It certainly does:

1
2
3
4
5
6
7
8
char cFilm[100];
strcpy(cFilm, "Some string data");

string sFilm = "BAD BAD BAD";

sFilm = cFilm;

cout << sFilm;  // prints "Some string data" 
Silly me... It appears I've placed my lines in the wrong order... Thank you for clarifying the validity of the assignment, Disch!
Topic archived. No new replies allowed.