-1

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

Dikhmasir
  • 1
  • 1
  • How do you handle errors? What error is reproted? You can use `std::perror("Some log text");` to get good information in standard error stream. Does the directory exist or should be created? – Marek R Apr 08 '22 at 10:24
  • 2
    There is not enough information to really understand the question, but it seems you want to concatenate a string onto a path? For which you'd probably use something like `std::stringstream`? Or perhaps the C++17 `std::filesystem::path`? – UnholySheep Apr 08 '22 at 10:24
  • Have you created the destination folder for the files? Are you sure the current working directory is what you expect it to be? – Alan Birtles Apr 08 '22 at 10:24
  • Side note: `std::ofstream` implicitly sets `ios::out` as default, so you don't need to specify it explicitly... – Aconcagua Apr 08 '22 at 10:25
  • Does the folder already exist? Can't create a file in a folder without a folder first. Unrealted, C++ isn't like Python, Java, etc. Just throwing guesses at the wall and seeing what sticks may be productive in other languages, but C++ is highly unfriendly to that modus-operandi. – WhozCraig Apr 08 '22 at 10:26
  • Are you aware of the sequence (comma) operator? (x, y) will first execute `x` (if that does anything at all), then `y` and yields the result of `y`. So `("/RPG GAME PERCOBAAN/Save Data", Save_Data)` is equivalent to just `Save_Data`, i. e. the contents you seem to want to store get the file name... – Aconcagua Apr 08 '22 at 10:31
  • `std::ofstream` is just another output stream like `std::cout` is as well, only the target differs. Have you tried `ofstream os("../RPG GAME PERCOBAAN/Save Data"); os << Save_Data;`? Note that you actually should check the stream state to avoid trying to write to a stream that failed to open: `if(os) { /* file has been opened correctly! */ os << Save_Data; } else { /* appropriate error handling! */ }`! – Aconcagua Apr 08 '22 at 10:35
  • what you seem to be looking for is to read a file name from the user, and then append that to some given path? – codeling Apr 08 '22 at 10:37
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Apr 08 '22 at 10:37
  • did the provided answer help you? if so, please consider upvoting / accepting it. if not, feel free to comment on what aspect ia still unclear! – codeling Apr 16 '22 at 17:36

1 Answers1

0

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);
codeling
  • 11,056
  • 4
  • 42
  • 71
  • thank you very much sir, it works. Then maybe I will ask more questions in the future. Please continue to help me sir. I will keep trying to learn c++, because I want to make an Ai. – Dikhmasir Apr 08 '22 at 13:11
  • if it helped, consider accepting and/or upvoting answers ;). Also, good luck with your AI! – codeling Apr 08 '22 at 13:27