C++17 file and directory operations <filesystem>

Table of contents

path operation

directory traversal

Document inspection and manipulation

Summarize


Every time I write C++ for directory operations, I usually adjust the SDK of the platform, especially the win32 api is very difficult to remember, so I check the documentation to see if there is a library that is as useful as the os module in Python.

So I found filesystem, never used it (the latest standard of my sixth edition C++ primer only introduces C++11)

The usage is organized as follows, for reference only

When you need to operate on files and directories on your computer, <filesystem>the header files in the C++17 standard library can provide you with convenient tools. It provides a series of classes and functions to handle file and directory operations, including path operations, directory traversal, file inspection, file operations, and more. This article will introduce you to <filesystem>the usage and functions of header files.

When you need to operate on files and directories on your computer, <filesystem>the header files in the C++17 standard library can provide you with convenient tools. It provides a series of classes and functions to handle file and directory operations, including path operations, directory traversal, file inspection, file operations, and more. This article will introduce you to <filesystem>the usage and functions of header files.

path operation

<filesystem>The class in the header file pathis used to represent the path of the file or directory. You can use various operators and functions to manipulate paths.

First, you can use /operators to combine paths. For example:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/";
  fs::path p2 = "John/Documents/";
  fs::path p3 = p1 / p2;
  std::cout << p3 << std::endl;
  return 0;
}

The above code will output "C:/Users/John/Documents", which is the result of combining the two paths.

You can also +=append a path to an existing path using the operator:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/";
  fs::path p2 = "John/Documents/";
  p1 += p2;
  std::cout << p1 << std::endl;
  return 0;
}

The above code will output "C:/Users/John/Documents", which is the result of appending the path to the existing path.

If you need to compare two paths, you can use ==the AND !=operator to compare them for equality:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/John/Documents/";
  fs::path p2 = "C:/Users/Jane/Documents/";
  if (p1 == p2) {
    std::cout << "The paths are equal" << std::endl;
  } else {
    std::cout << "The paths are not equal" << std::endl;
  }
  return 0;
}

The above code will output "The paths are not equal", this is because the two paths are not equal.

In addition to operators, paththe class provides some useful functions for manipulating paths. For example, you can use parent_path()a function to get the parent directory in a path:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  fs::path parent = p.parent_path();
  std::cout << parent << std::endl;
  return 0;
}

The above code will output "C:/Users/John/Documents", which is the parent directory in the path.

You can also use stem()a function to get the filename (without the extension) in the path:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
fs::path filename = p.stem();
std::cout << filename << std::endl;
return 0;
}

The above code will output `"report"`, which is the filename (extension not included).

If you need to get the extension in the path, you can use the `extension()` function:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  fs::path extension = p.extension();
  std::cout << extension << std::endl;
  return 0;
}

The above code will output ".txt", which is the extension of the file.

directory traversal

<filesystem>The header files provide functions to iterate over files and subdirectories within a directory. You can use directory_iteratorclasses to iterate over all the contents of a directory. For example, the following code will iterate over "C:/Users/John/Documents"all files and subdirectories in a directory:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    std::cout << entry.path() << std::endl;
  }
  return 0;
}

The above code will output the paths of all files and subdirectories in the directory.

If you just want to iterate over the files in a directory, you can use is_regular_file()the function to determine if an entry is a regular file:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    if (fs::is_regular_file(entry)) {
      std::cout << entry.path() << std::endl;
    }
  }
  return 0;
}

The above code will output the paths of all files in the directory.

If you only want to iterate over subdirectories within a directory, you can use is_directory()the function to determine whether an entry is a directory:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    if (fs::is_directory(entry)) {
      std::cout << entry.path() << std::endl;
    }
  }
  return 0;
}

The above code will output the paths of all subdirectories in the directory.

C++17's <filesystem>standard library provides functions to get the current working directory and create a directory.

The way to get the current working directory is through std::filesystem::current_paththe function , which returns an std::filesystem::pathobject of type, representing the path of the current working directory. For example:

#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path currentPath = std::filesystem::current_path();
    std::cout << "Current working directory is: " << currentPath << std::endl;

    return 0;
}

The way to create a directory is through std::filesystem::create_directorythe function , which accepts an std::filesystem::pathobject of type, representing the path of the directory to be created. For example:

#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path dirPath = "C:/mydir";
    if (std::filesystem::create_directory(dirPath)) {
        std::cout << "Directory created successfully." << std::endl;
    } else {
        std::cout << "Failed to create directory." << std::endl;
    }

    return 0;
}

Document inspection and manipulation

<filesystem><fstream>The and header files in the header file <iostream>provide functions to examine and manipulate files. For example, you can use exists()a function to check if a file exists:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  if (fs::exists(p)) {
    std::cout << "The file exists" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

The above code will output "The file exists"if the file exists.

You can use remove()functions to delete files:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  if (fs::exists(p)) {
    fs::remove(p);
    std::cout << "The file was successfully deleted" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

The above code will delete the file and output "The file was successfully deleted"if the file exists.

You can also use copy()functions to copy files:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path source = "C:/Users/John/Documents/report.txt";
  fs::path target = "C:/Users/John/Documents/report_copy.txt";
  if (fs::exists(source)) {
    fs::copy(source, target);
    std::cout << "The file was successfully copied" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

The above code will copy the file and output "The file was successfully copied"if the source file exists.

You can also use rename()functions to rename files:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path old_path = "C:/Users/John/Documents/report.txt";
  fs::path new_path = "C:/Users/John/Documents/report_renamed.txt";
  if (fs::exists(old_path)) {
    fs::rename(old_path, new_path);
    std::cout << "The file was successfully renamed" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

The above code will rename the file and output it "The file was successfully renamed", if the original file exists.

Summarize

<filesystem>The header files provide a powerful set of tools for working with the file system. You can use it to manage files and directories, get information about files and directories, and perform file operations. These features make working with files and directories easier and more portable, since <filesystem>header files can be used on multiple platforms.

Guess you like

Origin blog.csdn.net/weixin_40582034/article/details/129323112