How can I make a "return one directory" function?

Mar 1, 2022 at 3:09am
Let us say I want to represent the current working directory as a std::string and I need to return one directory back. What is the most efficient way of implementing it you can think of?
My train of thought goes something around the lines of:
>make sure the passed std::string is a valid directory
>check whether the current working directory is the root, then return the root
>else return the current working directory but only to the char before the last '/'
Is there a better way of implementing this?
Mar 1, 2022 at 3:16am
std::filesystem::current_path().string()
https://en.cppreference.com/w/cpp/filesystem/current_path
Mar 1, 2022 at 3:18am
JLBorges, that's not exactly what I needed, but still, thank you.
Mar 1, 2022 at 3:45am
What exactly do you mean by "return one directory"?

Are you wanting to get what the parent directory is for the current directory? If the current working dir is (Windows): C:\Dir_1_\Dir_2_ do you want C:\Dir_1_?

Take a gander at https://en.cppreference.com/w/cpp/filesystem/path under the decomposition section.
Mar 1, 2022 at 4:13am
Thank you a million, George P, that's exactly what I needed.
Topic archived. No new replies allowed.