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?

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());
commented: Thanks :) +2

try :

myfile.open (string1.c_str());

Work :) thanks.

But why does it work? whats the meaning?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.