txxnano
txxnano

Reputation: 151

Ofstream, use variable to the name

Sorry for my english, but speak spanish...

In this week, study and work for this proyect, I want create a software to make files(.us)...

Example

char name[50]; //Or string
cin>>name;
 
ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name+"\\Classes\\_PlayerPawn.us");

But the compiler has error in the Operator binary plus

Any alternative, examples or something for create the file in specific directory

Upvotes: 1

Views: 1413

Answers (2)

Seth Carnegie
Seth Carnegie

Reputation: 75150

Either side of operator+ must be a std::string1 for operator+ to concatenate strings:

string name;
cin >> name;

ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name + "\\Classes\\_PlayerPawn.us");

And use std::string for this stuff; with std::string there's no danger of buffer overflows that you get with char*.


1 Actually it just needs to be a class type that supports operator+, not specifically std::string, but then you have no idea what it will do.

Upvotes: 3

Matt
Matt

Reputation: 10564

I believe you want name to be a std::string - otherwise, name + [suffix] will try to add the suffix string to the array and will not compile. If you really want to keep the name as an array, you should use strcat to append the strings together.

Upvotes: 3

Related Questions