'How to create a txt file in another directory using Fstream?

I want to create a txt file in another folder using fstream. You must know rpg games, right? there must be a save data system. to save our progress in the game, don't be happy if the save data file is mixed with the exe file. I've tried various ways.

for example

cin >> Save_Data;
ofstream Char_Pertama (("/RPG GAME PERCOBAAN/Save Data", Save_Data), ios :: out);

cin >> Save_Data;
ofstream Char_Pertama ("/RPG GAME PERCOBAAN/Save Data"); (Save_Data, ios :: out);

                     

I have tried this one too...

cin >> Save_Data;
ofstream Char_Pertama ("../RPG GAME PERCOBAAN/Save Data/Save_Data", ios :: out);

But,the txt file is not up to us to name it. I wish, we can make the filename as we like.

please help me to solve this, your suggestions will be very helpful for me



Solution 1:[1]

You seem to be looking for a way to read in a file name, and then concatenate that filename to some fixed path. Try

std::string basepath("../RPG GAME PERCOBAAN/Save Data/");
std::string filename;
cin >> filename;
ofstream Char_Pertama (basepath+filename, ios::out);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codeling