Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one get user's home directory using C++17 std::filesystem?

Tags:

c++

c++17

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?

like image 361
Amani Avatar asked May 28 '19 00:05

Amani


People also ask

How do you find what is your home directory?

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.

How do I find a directory in C++?

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.

What is home directory file?

A home directory is a file system directory on a multi-user operating system containing files for a given user of the system.

Where is the file system library in C++17?

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 ...

Where can I find C++17 examples in Visual Studio 2017?

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.

Where can I find the final spec in the C++17 draft?

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


Video Answer


1 Answers

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.

like image 54
Yakk - Adam Nevraumont Avatar answered Oct 03 '22 00:10

Yakk - Adam Nevraumont