How do I insert into string into myfile.open?
So I am making a program that allows someone to make their own text file so I wanted to insert a string into this piece of code
I tried myfile.open( name + ".txt"); but that doesn't work, Here is the full section of the code and any help is appreciated !
1 2 3 4 5 6 7
|
cout << "Creating File!" << endl;
if (yu == 1) {
ofstream myfile;
ofstream myfile;
myfile.open(name + ".txt");
myfile << "Apples are great!.\n";
myfile.close();
|
Ignore the yu and Apples are great that's just some test stuff
I tried myfile.open( name + ".txt"); |
So long as you use an up-to-date compiler and enable C++11 that should work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string name = "banana";
cout << "Creating File!" << endl;
ofstream myfile(name + ".txt");
myfile << "Apples are great!\n";
return 0;
}
|
Older compilers may expect a C-string as the filename.
For example,
ofstream myfile((name + ".txt").c_str());
Topic archived. No new replies allowed.