How to create ofstream file with name of variable?

后端 未结 3 1954
甜味超标
甜味超标 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 06:43

    Like this:

    #include 
    #include 
    
    std::string filename;
    std::getline(std::cin, filename);
    std::ofstream fout(filename);
    

    In older versions of C++ the last line needs to be:

    std::ofstream fout(filename.c_str());
    

提交回复
热议问题