How to create ofstream file with name of variable?

后端 未结 3 1952
甜味超标
甜味超标 2021-01-17 06:08
char NAME[256];
cin.getline (NAME,256);
ofstream fout(\"NAME.txt\"); //NAME???????

What i need to do to create file with NAME name?

3条回答
  •  一生所求
    2021-01-17 07:03

    You can try this.

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        string fileName;
        cout << "Give a name to your file: ";
        cin >> fileName;
        fileName += ".txt"; // important to create .txt file.
        ofstream createFile;
        createFile.open(fileName.c_str(), ios::app);
        createFile << "This will give you a new file with a name that user input." << endl;
        return 0;
    }
    

提交回复
热议问题