[C++17] How to Use std::filesystem::path | Constructing and Concatenating Paths

目次

Introduction

The <filesystem> library introduced in C++17 provides the std::filesystem::path class for handling file system paths uniformly. This path object abstracts differences in path separators between operating systems (such as \ in Windows and / in Linux/macOS), enabling highly portable path operations. In this article, I will explain the basic usage of the path object: construction (creation) and concatenation.

Prerequisite: What is C++17?

C++17 is the C++ language standard formalized in 2017. Since the <filesystem> library was introduced in C++17, you need a compiler that supports C++17 and appropriate compiler settings (and linker settings in some environments) to use it.

Sample Code for Path Construction and Concatenation

Complete Code

#include <iostream>
#include <filesystem>
#include <string>

// Alias for filesystem namespace
namespace fs = std::filesystem;

int main() {
    // --- 1. Construction from various string types ---
    // From C-style string literal
    fs::path path_from_char_literal = "C:/Users/Public";
    
    // From std::string object
    std::string string_path = "Documents";
    fs::path path_from_string(string_path);

    std::cout << "From C-string: " << path_from_char_literal << std::endl;
    std::cout << "From std::string: " << path_from_string << std::endl;
    
    std::cout << "\n--- 2. Path Concatenation ---" << std::endl;

    // --- a. Concatenation with / operator ---
    // Concatenate path object and string literal
    fs::path combined_path1 = path_from_char_literal / "Music";
    std::cout << "/ operator: " << combined_path1 << std::endl;

    // --- b. Concatenation with /= operator ---
    fs::path base_path = "D:/Data";
    base_path /= "Images"; // base_path itself is modified
    base_path /= "2025";
    std::cout << "/= operator: " << base_path << std::endl;
    
    return 0;
}

Execution Result

(The path display format varies depending on the environment)

From C-string: "C:/Users/Public"
From std::string: "Documents"

--- 2. Path Concatenation ---
/ operator: "C:/Users/Public/Music"
/= operator: "D:/Data/Images/2025"

Code Explanation

1. Constructing path Objects

The std::filesystem::path object can be constructed from various types of strings:

  • const char* (C-style string literal)
  • std::string
  • Wide character versions (const wchar_t*, std::wstring)
  • UTF-8 versions (const char8_t*, std::u8string), etc.

You can easily generate a path object from these strings by passing them to the constructor or using the assignment operator =.

2. Path Concatenation with / and /= Operators

The most intuitive and useful feature of the path object is path concatenation using the / operator.

  • / Operator: Returns a new path object that concatenates two path elements, like path1 / path2. You can freely combine path objects, std::string, and string literals.
  • /= Operator: Appends the path element on the right side to the path object on the left side, like base_path /= "subdir". The object on the left side itself is modified.

These operators automatically determine whether the end of path1 ends with a separator (/ or \) and insert a single separator only if necessary. Even if you concatenate "C:/Users/" and "Music", it will correctly become "C:/Users/Music" instead of "C:/Users//Music".

Summary

In this article, I explained the basic usage of std::filesystem::path in C++17, specifically object construction and concatenation.

  • Path objects can be easily created from various strings.
  • Using the / and /= operators allows for intuitive and safe path concatenation without worrying about OS separators.
  • Since manual string concatenation for paths is prone to bugs like duplicate or missing separators, it is best practice to actively use path objects and the / operator in environments where <filesystem> is available.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次