Home  >  Article  >  Backend Development  >  How to delete a file using C++?

How to delete a file using C++?

WBOY
WBOYOriginal
2024-06-03 10:12:57799browse

How to delete files in C++? Use the remove function to delete files, its prototype is int remove(const char* filename); use std::filesystem::remove function to delete files, its prototype is std::error_code remove(const std::filesystem::path& path);

How to delete a file using C++?

How to delete files in C++

C++ provides various functions and methods to delete files. In this article, we will introduce how to use the remove function and the std::filesystem::remove function to delete files, and provide a practical case.

remove Function

remove function is a function defined in the C++ standard library and is used to delete files. Its prototype is as follows:

int remove(const char* filename);

where:

  • filename is the path of the file to be deleted.

remove The function returns an integer indicating the status of the operation:

  • 0 indicates successful deletion.
  • A non-0 value indicates deletion failure.

Practical case: Use the remove function to delete files

The following is a example of using the remove function to delete files Code example:

#include <cstdio>

int main() {
  // 要删除的文件路径
  const char* filename = "test.txt";

  // 删除文件
  int result = remove(filename);

  // 检查操作结果
  if (result == 0) {
    printf("文件 %s 删除成功!\n", filename);
  } else {
    printf("文件 %s 删除失败,错误码:%d\n", filename, result);
  }

  return 0;
}

std::filesystem::remove Function

Introduced in the C++ standard library of C++17 and later versions Added the std::filesystem header file, which provides a more modern and high-level API for file system operations. We can use the std::filesystem::remove function to delete files. Its prototype is as follows:

std::error_code remove(const std::filesystem::path& path);

where:

  • path is the path of the file to be deleted.

std::filesystem::remove The function returns a std::error_code object representing the status of the operation. We can use the std::error_code::value() method to get the error code.

Practical case: Use std::filesystem::remove function to delete files

The following is a usestd::filesystem: Code example for :remove function to delete files:

#include <filesystem>

int main() {
  // 要删除的文件路径
  std::filesystem::path filename = "test.txt";

  // 删除文件
  std::error_code ec;
  std::filesystem::remove(filename, ec);

  // 检查操作结果
  if (!ec) {
    std::cout << "文件 " << filename << " 删除成功!" << std::endl;
  } else {
    std::cout << "文件 " << filename << " 删除失败,错误码:" << ec.value() << std::endl;
  }

  return 0;
}

The above is the detailed content of How to delete a file using C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn