I know that this is not a new topic at all. But this blog in its roots is to document what I learn, and I haven’t used the filesystem library up until a few weeks ago. After the initial encounter, I deliberately spent a bit more time exploring it and I want to share what I found.
I don’t want to go over the C++ Reference documentation and I also don’t want to simply repeat what Bartek already shared here.
Bạn đang xem: My late discovery of std::filesystem – Part I
I rarely use C++ to manipulate the filesystem. That usually comes up with Python. So I decided to go through my Python use cases and see how that would in C++ with the std::filesystem library. Which was introduced in C++17, but its roots are back in the boost::filesystem library.
Get the current filename
By current file, I don’t mean the executable file, but the source code file. This is not something that we are going to achieve with std::filesystem. You either need to use the standard __FILE__ macro, or you need to use std::source_location::current()::file_name. Let’s see a little example:
Even though we don’t do this with std::filesystem, I wanted to include it, because requiring the current filename is often needed for the following use cases.
Get the directory of a file
Now that we have the absolute path of the current file, let’s see how to get the directory of it.
Nothing is simpler than that!
Get the absolute path
Xem thêm : When are mice considered old?
First of all, if we need the current directory, we can use std::filesystem::current_path(). But if that’s not the case and we already have an absolute path of a file anywhere even just as a string, we can use remove_filename().
Get the relative path
You might say that you are not interested in the absolute path, but rather in the relative. Let’s assume that we have a file called /Users/sandor/personal/dev/dojos/Racing-Car-Katas/Cpp/TextConverter/tests/HtmlTextConverter_Test.cpp. Your first idea might be to try std::filesystem::path::relative_path() but then you realize that it doesn’t take any parameters! What does relative mean then? Well, it only removes the prefix that signals the root, e.g. / or C:…
If you want to get the relative path compared to another path, then instead of the above member function we have to use a free function called std::filesystem::relative.
As we can see, the only question is which path goes first. The correct way to read this is “give me the relative path of the left-hand path compared to the right-hand path”.
In my opinion, a nice API could have used a member function, such as std::filesystem::path::relative_to(otherPath). But there must have been other considerations that I’m not aware of.
Step up a directory
If you have a path at your hands and you want the path of the parent directory, you don’t have a difficult task, just use the parent_path() member function.
Check if a file is a file or directory
Let’s check now if a file or directory exists. To do so, we leave behind the RacingKar katas and we are going to use the standard library to create some files and directories. With std::filesystem::create_directory, it’s easy to create a new folder. With std::ofstream we can create a file(stream) and with std::ofstream::put we can add a character to it.
Xem thêm : Parks, Recreation and Historic Preservation
As you can see, by calling std::filesystem::exists(path), it’s easy to check whether a file or directory exists and there are different additional query functions available to check if a file object is a directory, a block, character or regular file. These file types are defined in the POSIX standard.
Copy or rename a file
In the next example, after creating some files and directories, we are going to first copy a directory, then some files and then we will rename a file. To facilitate our example, we’ll also use std::filesystem::remove to remove directories and files.
In the “copy a file to…?” part, we can observe that when we want to copy a file, the destination is not the destination directory, but the destination path. If you don’t pay attention, you might copy a file to a place which was meant to be a directory.
But it can only happen if you haven’t created the destination directory yet. It’s important to note, that you cannot use std::filesystem::copy to copy to a non-existing directory. You have to make sure that it exists. Even though as a third, optional parameter, it takes std::filesystem::copy_options, seemingly there is no option to create automatically the needed directory.
While std::filesystem::copy can be used to copy both files and directories, there is also std::filesystem::copy_file which can only copy a single file. Its name is more expressive, but that’s not the only difference. While copy is a void function, copy_file returns a boolean to show if a copy was successful (true) or not (false).
Conclusion
Today, I shared with you a part of what I learned about the std::filesystem library. I must tell you that I found it pretty usable, despite the fact that sometimes I could have imagined a more intuitive API. We had a look into how to navigate up on a path, how to remove a filename from it and also we also checked how to copy, rename or delete files.
Next week, we are going to discuss how to iterate over a directory structure, a quite common operation when you have to apply some changes to a whole repository. Stay tuned!
Connect deeper
If you liked this article, please
- hit on the like button,
- subscribe to my newsletter
- and let’s connect on Twitter!
Nguồn: https://buycookiesonline.eu
Danh mục: Info