0

I need to create a new txt file using ofstream, but I need to use a string to name the file.

For example:

string string1 = "testfile.txt";
ofstream myfile;
myfile.open (string1);

but when I try, it says I can do this because string is not a valid variable type for an 'open' call.

Any ideas?

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Danny_501
2

I need to create a new txt file using ofstream, but I need to use a string to name the file.

For example:

string string1 = "testfile.txt";
ofstream myfile;
myfile.open (string1);

but when I try, it says I can do this because string is not a valid variable type for an 'open' call.

Any ideas?

try :

myfile.open (string1.c_str());
Votes + Comments
Thanks :)
0

try :

myfile.open (string1.c_str());

Work :) thanks.

But why does it work? whats the meaning?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.