跳转至

C++17 文件与目录操作<filesystem>

当你需要对计算机上的文件和目录进行操作时,C++17标准库中的头文件可以为你提供方便的工具。它提供了一系列的类和函数来处理文件和目录的操作,包括路径操作、目录遍历、文件检查、文件操作等等。本文将为你介绍头文件的使用方法和功能。

  • 路径操作

头文件中的path类用于表示文件或目录的路径。你可以使用各种运算符和函数来操作路径。例如对路径的组合和判断相等:

C++
#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;
}
C++
#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;
}
C++
#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;
}

除了运算符,path类还提供了一些有用的函数来操作路径。例如:

C++
#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;
}
C++
#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;
}
C++
#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;
}
  • 目录遍历

头文件提供了一些函数来遍历目录中的文件和子目录。你可以使用directory_iterator类来遍历目录中的所有内容。

C++
#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;
}
C++
#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;
}
C++
#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;
}

C++17的标准库提供了获取当前工作目录和创建目录的功能。

C++
1
2
3
4
5
6
7
8
9
#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;
}
C++
#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;
}
  • 文件操作

头文件中的头文件提供了一些函数来检查和操作文件。

C++
#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;
}
C++
#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;
}
C++
#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;
}
C++
#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;
}