remove

remove

在头文件中定义
int remove(const char * fname);

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

如果该文件当前由该进程或其他进程打开,则此函数的行为是由实现定义的(特别是,POSIX系统取消链接文件名,尽管在上次运行进程关闭文件之前文件系统空间不会被回收; Windows会不允许删除文件)。

参数

FNAME-指向包含标识要删除的文件的路径的以空字符结尾的字符串

返回值

​0​ 一旦成功或错误发生非零值。

注意

POSIX为这个函数的行为指定了许多额外的细节。

Example

#include <stdio.h> int main(void) { FILE* fp = fopen("file1.txt", "w" // create file if(!fp) { perror("file1.txt" return 1; } puts("Created file1.txt" fclose(fp int rc = remove("file1.txt" if(rc) { perror("remove" return 1; } puts("Removed file1.txt" fp = fopen("file1.txt", "r" // Failure: file does not exist if(!fp) perror("Opening removed file failed" rc = remove("file1.txt" // Failure: file does not exist if(rc) perror("Double-remove failed" }

输出:

Created file1.txt Removed file1.txt Opening removed file failed: No such file or directory Double-remove failed: No such file or directory

参考

  • C11标准(ISO / IEC 9899:2011):