Home > Backend Development > C++ > body text

How to delete a file using C++?

WBOY
Release: 2024-06-03 10:12:57
Original
728 people have browsed it

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);
Copy after login

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;
}
Copy after login

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);
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!