Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::remove

      From cppreference.com
      <cpp‎ |io‎ |c
       
       
       
       
      Defined in header<cstdio>
      int remove(constchar* pathname);

      Deletes the file identified by the character string pointed to bypathname.

      If the file is currently open by any process, the behavior of this function is implementation-defined. POSIX systems unlink the file name (directory entry), but the filesystem space used by the file is not reclaimed while it is open in any process and while other hard links to the file exist. Windows does not allow the file to be deleted in such cases.

      Contents

      [edit]Parameters

      pathname - pointer to a null-terminated string containing the path identifying the file to delete

      [edit]Return value

      0 upon success or non-zero value on error.

      [edit]Notes

      POSIX specifies many additional details for the behavior of this function.

      The standard library also defines a function templatestd::remove taking a pair of iterators and a value, this overload is one of the standardalgorithms.

      [edit]Example

      Run this code
      #include <cstdio>#include <cstdlib>#include <fstream>#include <iostream> int main(){// create a file, check success using operator! of temporary stream objectif(!std::ofstream("file1.txt").put('a')){std::perror("Error creating file1.txt");returnEXIT_FAILURE;}std::cout<<std::ifstream("file1.txt").rdbuf()<<'\n';// print file     std::remove("file1.txt");// delete file if(!std::ifstream{"file1.txt"})// uses operator! of temporary stream object{std::perror("Error opening deleted file");returnEXIT_FAILURE;}returnEXIT_SUCCESS;}

      Possible output:

      aError opening deleted file: No such file or directory

      [edit]See also

      (C++17)(C++17)
      removes a file or empty directory
      removes a file or directory and all its contents, recursively
      (function)[edit]
      renames a file
      (function)[edit]
      C documentation forremove
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/c/remove&oldid=168453"

      [8]ページ先頭

      ©2009-2025 Movatter.jp