previous | start | next

Convert a C++ string to a C string

Why? Because some functions require a C string, not a C++ string. For example, to open an ifstream requires the file name be a C-string.

How?

int main() { string fname; char cfname[130]; cout << "Enter a file name (no spaces): "; cin >> fname; strcpy(cfname, fname.c_str()); ifstream ifs; ifs.open(cfname); // NOT ifs.open(fname); }


previous | start | next