Search code examples
c++ofstream

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


Solution

  • 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.