I need to obtain the current user's home directory and then append to it paths to other directories. Since the app is cross-platform then the approach should take this into consideration. I have been looking for a solution without success. Is there a way to get current user's home directory using C++17 std::filesystem?
Starting with Windows Vista, the Windows home directory is \user\username. In prior Windows versions, it was \Documents and Settings\username. In the Mac, the home directory is /users/username, and in most Linux/Unix systems, it is /home/username.
Use the getcwd Function to Get Current Directory If the call is successful, the stored name of the current directory can be printed by accessing the char buffer.
A home directory is a file system directory on a multi-user operating system containing files for a given user of the system.
The library is located in the <filesystem> header. It uses namespace std::filesystem. The final paper is P0218R0: Adopt the File System TS for C++17 but there are also others like P0317R1: Directory Entry Caching, PDF: P0430R2–File system library on non-POSIX-like operating systems, P0492R2 ...
Hopefully by the end of the year VS 2017 will fully implement C++17 (and STL) All the examples can be found on my Github: github.com/fenbf/articles/cpp17. I've used Visual Studio 2017 Update 2. The core part of the library is the path object. Just pass it a string of the path, and then you have access to lots of useful functions.
All in all, you can find the final spec in the C++17 draft: the "filesystem" section, 30.10. We have three/four core parts: files manipulation: copy, move, create, symlinks
No.
The only special directories you are handed is a directory suitable for temporary files, and the current directory.
You'll have to write a OS specific wrapper for getting the home directory. I, personally, think that is a bad idea: rather, you should have "user app settings" directory function, "user document'" directory function, etc (as determined by your application needs). Where those directories are compared to the home directory vary on a OS by OS basis.
In my experience, also having an API for storing a "registry-like property set" is a good idea (a string-string map with serialization code on the values, for example). It can map to plists on macOS, registry on windows, and something else on linux.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With