Relative path with fstream C ++

I am trying to upload a file using fstream. The code is as follows:

file.open("../levels/level0.lvl"); if (file.is_open()) { while (!file.eof()) { std::getline(file, Str); list = ReadLine(Str, list); } } 

But he does not load anything. Yes, only if the path is absolute. How can I make the path relative?

The "level" folders are located in the debug folder. same folder as exe.

+5
source share
2 answers

The folders "levels" are located in the debug folder. In the same folder as exe.

It does not matter in which position the levels folder refers to the executable path.
The appropriate folder for determining the relative path is the working directory from which your executable file is actually launched.


See here: fstream also does not resolve the path .

+6
source

Path processing is OS dependent. The right way to handle this is to add a user method that defines the path to your application, and then use that path. For example, you can add the command line option --level-file=<path> . Then your program can read the path from this parameter and pass it to the fstream constructor.

See my answer to this question: fooobar.com/questions/286570 / ...

0
source

Source: https://habr.com/ru/post/1212046/


All Articles