Search code examples
c++c++11codeblocksofstream

How to create ofstream file with the name of (integer type) variable?


int a
cin>>a;
ofstream file;
file.open("a.txt");

I want to create a .txt file with the code entered by the user. Please help.


Solution

  • Need to convert the input to std::string:

     int a;
     cin>>a;
     ofstream file(std::to_string(a) + ".txt");