C++: Difference between revisions

305 bytes added ,  6 February 2020
Line 95: Line 95:
Note if you use g++ <= version 9, you will need to add the flag <code>-lstdc++fs</code>.<br>
Note if you use g++ <= version 9, you will need to add the flag <code>-lstdc++fs</code>.<br>
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
using std::filesystem::path;
// Initialization
path my_path = "my_dir/my_file";
// or my_path = path("my_dir") / "my_file";


// Append to path
// Append to path
path("foo") / "bar"; // "foo/bar"
path("foo") / "bar"; // path("foo/bar")
path("foo") / "/bar"; // "/bar"
path("foo") / "/bar"; // path("/bar")
 
// Print
std::cout << my_path << std::endl; // prints "my_dir/my_file" with quotes
std::cout << my_path.string() << std::endl; // prints my_dir/my_file without quotes
</syntaxhighlight>
</syntaxhighlight>