Converting parameters

Mar 1, 2011 at 8:38pm
Hi! Can anyone please tell me how can I make this code work?

Basically it's just a program which creates a file.
1
2
3
4
5
6
FILE * pFile;
string filename;
cout << "Name the project" << endl;
cin >> filename; // This gets the name of the file you want to create. 
pFile = fopen (filename,"w"); // This creates the file, but fails. 
cannot convert parameter 1 from 'std::string' to 'const char *'
fclose (pFile);


What should I do?
Thanks.
Last edited on Mar 1, 2011 at 8:38pm
Mar 1, 2011 at 8:54pm
you must use the c_str function to get a char pointer:

 
pFile = fopen(filename.c_str(),"w");
Mar 1, 2011 at 8:59pm
Thanks a lot! It works.
Topic archived. No new replies allowed.