1、删除文件
要使用C++ 删除文件,需要使用int remove(const char * filename);方法,filename为要删除的文件名,可以为一目录。如果参数filename 为一文件,则调用unlink()处理;若参数filename 为一目录,则调用rmdir()来处理。删除成功则返回0,失败则返回-1,错误原因存于errno。C++中头文件是#include <cstdio>。
#include <cstdio> // 包含remove函数
#include<iostream>
using namespace std;
int main() {
const char* filename = "example.txt";
if (remove(filename) == 0) {
cout << "文件删除成功: " << filename << endl;
} else {
perror("文件删除失败");
}
return 0;
}
错误代码:
1)EROFS 欲写入的文件为只读文件。
2)EFAULT 参数filename 指针超出可存取内存空间。
3)ENAMETOOLONG 参数filename 太长。
4)ENOMEM 核心内存不足。
5)ELOOP 参数filename 有过多符号连接问题。
6)EIO I/O存取错误。
2、删除文件夹
除了能删除文件,也可以使用int rmdir( const char *dirname );删除文件夹。删除成功则返回0,失败则返回-1,错误原因存于errno。但是,删除的文件夹必须为空:
1)删除空目录
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
const char* dirname = "example_dir";
if (rmdir(dirname) == 0) {
cout << "目录删除成功: "
<< dirname << endl;
} else {
perror("目录删除失败");
}
return 0;
}2)使用C++17 <filesystem>库
#include <filesystem>
#include <iostream>
using namespace std;
int main() {
filesystem::path file = "example.txt";
try {
if (filesystem::remove(file)) {
cout << "文件/目录删除成功: "
<< file << endl;
} else {
cout << "文件/目录不存在: "
<< file << endl;
}
} catch (const filesystem::filesystem_error& e) {
cerr << "错误: " << e.what() << endl;
}
return 0;
}3、删除某个目录及目录下的所有子目录和文件
删除某个目录及目录下的所有子目录和文件。remove()只能删除某个文件或者空目录,要想要删除某个目录及其所有子文件和子目录,要使用递归进行删除。通过<filesystem>库(C++17引入),可以方便地删除文件或目录,包括递归删除目录。
1)使用C标准库
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
using namespace std;
void error_quit( const char *msg )
{
perror( msg );
exit( -1 );
}
void change_path( const char *path )
{
printf( "Leave %s Successed . . .\n", getcwd( NULL, 0 ) );
if ( chdir( path ) == -1 )
error_quit( "chdir" );
printf( "Entry %s Successed . . .\n", getcwd( NULL, 0 ) );
}
void rm_dir( const char *path )
{
DIR *dir;
struct dirent *dirp;
struct stat buf;
char *p = getcwd( NULL, 0 );
if ( (dir = opendir( path ) ) == NULL )
error_quit( "OpenDir" );
change_path( path );
while ( dirp = readdir( dir ) )
{
if ( (strcmp( dirp->d_name, "." ) == 0) || (strcmp( dirp->d_name, ".." ) == 0) )
continue;
if ( stat( dirp->d_name, &buf ) == -1 )
error_quit( "stat" );
if ( S_ISDIR( buf.st_mode ) )
{
rm_dir( dirp->d_name );
/*if(rmdir(dirp->d_name)==-1)
* error_quit("rmdir");
* printf("rm %s Successed . . .\n",dirp->d_name);*/
continue;
}
if ( remove( dirp->d_name ) == -1 )
error_quit( "remove" );
printf( "rm %s Successed . . .\n", dirp->d_name );
}
closedir( dir );
change_path( p );
if ( rmdir( path ) == -1 )
error_quit( "rmdir" );
printf( "rm %s Successed . . .\n", path );
}
int main( int argc, char **argv )
{
// if ( argc < 2 )
//{
//fprintf( stderr, "<用法>: %s + pathname", argv[0] );
//return(-1);
//}
rm_dir("/tmp/");
return(0);
}
2)使用C++17 <filesystem>库
#include <filesystem>
#include <iostream>
using namespace std;
int main() {
filesystem::path dir = "example_dir";
try {
filesystem::remove_all(dir);
cout << "目录及其内容已删除: "
<< dir << endl;
} catch (const filesystem::filesystem_error& e) {
cerr << "错误: " << e.what() << endl;
}
return 0;
}