new learner
new learner

Reputation: 49

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.

Upvotes: 0

Views: 356

Answers (1)

Eugene
Eugene

Reputation: 7688

Need to convert the input to std::string:

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

Upvotes: 1

Related Questions