You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/standard-library/file-system-navigation.md
+16-15Lines changed: 16 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
2
title: "File System Navigation"
3
-
ms.date: "11/04/2016"
3
+
description: "How to use the C++ Standard library filesystem APIs to navigate the file system."
4
+
ms.date: "04/13/2020"
4
5
ms.assetid: f7cc5f5e-a541-4e00-87c7-a3769ef6096d
5
6
---
6
7
# File System Navigation
7
8
8
-
The \<filesystem> header implements the C++ File System Technical Specification ISO/IEC TS 18822:2015 (Final draft: [ISO/IEC JTC 1/SC 22/WG 21 N4100](https://wg21.link/n4100)) and has types and functions that enable you to write platform-independent code for navigating the file system. Because it is cross-platform, it contains APIs that are not relevant for Windows systems. For example, this means that`is_fifo(const path&)` always returns **false** on Windows.
9
+
The \<filesystem> header implements the C++ File System Technical Specification ISO/IEC TS 18822:2015 (Final draft: [ISO/IEC JTC 1/SC 22/WG 21 N4100](https://wg21.link/n4100)) and has types and functions that enable you to write platform-independent code for navigating the file system. Because it's cross-platform, it contains APIs that aren't relevant for Windows systems. For example, `is_fifo(const path&)` always returns **false** on Windows.
9
10
10
11
## Overview
11
12
@@ -17,7 +18,7 @@ Use the \<filesystem> APIs for the following tasks:
17
18
18
19
- compose, decompose, and compare paths
19
20
20
-
- create, copy and delete directories
21
+
- create, copy, and delete directories
21
22
22
23
- copy and delete files
23
24
@@ -27,7 +28,7 @@ For more information about File IO using the Standard Library, see [iostream Pro
27
28
28
29
### Constructing and composing paths
29
30
30
-
Paths in Windows (since XP) are stored natively in Unicode. The [path](../standard-library/path-class.md) class automatically performs all necessary string conversions. It accepts arguments of both wide and narrow character arrays, as well as `std::string` and `std::wstring` types formatted as UTF8 or UTF16. The `path` class also automatically normalizes path separators. You can use a single forward slash as a directory separator in constructor arguments. This enables you to use the same strings to store paths in both Windows and UNIX environments:
31
+
Paths in Windows (since XP) are stored natively in Unicode. The [path](../standard-library/path-class.md) class automatically does all necessary string conversions. It accepts arguments of both wide and narrow character arrays, and both `std::string` and `std::wstring` types formatted as UTF8 or UTF16. The `path` class also automatically normalizes path separators. You can use a single forward slash as a directory separator in constructor arguments. This separator lets you use the same strings to store paths in both Windows and UNIX environments:
The path class has several methods that return information about various parts of the path itself, as distinct from the file system entity it might refer to. You can get the root, the relative path, the file name, the file extension, and more. You can iterate over a path object to examine all the folders in the hierarchy. The following example shows how to iterate over a path (not the directory it refers to), and to retrieve information about its parts.
48
+
The path class has several methods that return information about various parts of the path itself. This information is distinct from the information about the file system entity it might refer to. You can get the root, the relative path, the file name, the file extension, and more. You can iterate over a path object to examine all the folders in the hierarchy. The following example shows how to iterate over a path object. And, how to retrieve information about its parts.
48
49
49
50
```cpp
50
51
// filesystem_path_example.cpp
51
-
// compile by using: /EHsc
52
+
// compile by using: /EHsc /W4 /permissive- /std:c++17 (or /std:c++latest)
52
53
#include<string>
53
54
#include<iostream>
54
55
#include<sstream>
55
56
#include<filesystem>
56
57
57
58
usingnamespacestd;
58
-
usingnamespacestd::experimental::filesystem;
59
+
usingnamespacestd::filesystem;
59
60
60
61
wstring DisplayPathInfo()
61
62
{
@@ -82,7 +83,7 @@ wstring DisplayPathInfo()
82
83
return wos.str();
83
84
}
84
85
85
-
intmain(int argc, char* argv[])
86
+
intmain()
86
87
{
87
88
wcout << DisplayPathInfo() << endl;
88
89
// wcout << ComparePaths() << endl; // see following example
@@ -113,7 +114,7 @@ extension() = .txt
113
114
114
115
### Comparing paths
115
116
116
-
The `path` class overloads the same comparison operators as `std::string` and `std::wstring`. When you compare two paths, you are performing a string comparison after the separators have been normalized. If a trailing slash (or backslash) is missing it is not added and affects the comparison. The following example demonstrates how path values compare:
117
+
The `path` class overloads the same comparison operators as `std::string` and `std::wstring`. When you compare two paths, you make a string comparison after the separators have been normalized. If a trailing slash (or backslash) is missing, it isn't added, and that affects the comparison. The following example demonstrates how path values compare:
117
118
118
119
```cpp
119
120
wstring ComparePaths()
@@ -148,22 +149,22 @@ To run this code, paste it into the full example above before `main` and uncomme
148
149
149
150
### Converting between path and string types
150
151
151
-
A `path` object is implicitly convertible to `std::wstring` or `std::string`. This means you can pass a path to functions such as [wofstream::open](../standard-library/basic-ofstream-class.md#open), as shown in this example:
152
+
A `path` object is implicitly convertible to `std::wstring` or `std::string`. It means you can pass a path to functions such as [wofstream::open](../standard-library/basic-ofstream-class.md#open), as shown in this example:
152
153
153
154
```cpp
154
155
// filesystem_path_conversion.cpp
155
-
// compile by using: /EHsc
156
+
// compile by using: /EHsc /W4 /permissive- /std:c++17 (or /std:c++latest)
156
157
#include<string>
157
158
#include<iostream>
158
159
#include<fstream>
159
160
#include<filesystem>
160
161
161
162
usingnamespacestd;
162
-
usingnamespacestd::experimental::filesystem;
163
+
usingnamespacestd::filesystem;
163
164
164
-
int main(int argc, char* argv[])
165
+
int main()
165
166
{
166
-
wchar_t* p = L"C:/Users/Public/Documents";
167
+
const wchar_t* p{ L"C:/Users/Public/Documents" };
167
168
path filePath(p);
168
169
169
170
filePath /= L"NewFile.txt";
@@ -203,4 +204,4 @@ Press Enter to exit
203
204
204
205
The \<filesystem> header provides the [directory_iterator](../standard-library/directory-iterator-class.md) type to iterate over single directories, and the [recursive_directory_iterator](../standard-library/recursive-directory-iterator-class.md) class to iterate recursively over a directory and its subdirectories. After you construct an iterator by passing it a `path` object, the iterator points to the first directory_entry in the path. Create the end iterator by calling the default constructor.
205
206
206
-
When iterating through a directory, there are several kinds of items you might encounter, including but not limited to directories, files, symbolic links, and socket files. The `directory_iterator` returns its items as [directory_entry](../standard-library/directory-entry-class.md) objects.
207
+
When iterating through a directory, there are several kinds of items you might discover. These items include directories, files, symbolic links, socket files, and others. The `directory_iterator` returns its items as [directory_entry](../standard-library/directory-entry-class.md) objects.
0 commit comments