Linux下C/C++删除指定文件夹下所有文件

#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <cstring>
#include <cstdio> // remove
using namespace std;

// function:获取指定路径目录下所有文件名,存放在filenames中
void getFiles(string path, vector<string>& filenames)
{
    
    
	DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str()))){
    
    
        cout<<"Folder doesn't Exist!"<<endl;
        return;
    }
    while((ptr = readdir(pDir))!=0) {
    
    
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
    
    
            filenames.push_back(path + "/" + ptr->d_name);
    	}
    }
    closedir(pDir);
}


int main(int argc, char** argv)
{
    
    
	string filePath = "./all_topic_data";
	vector<string> files;
 
	//获取该路径下的所有文件
	getFiles(filePath, files);
 
	char str[30];
	int size = files.size();
	int nullSize = 0;
	for (int i = 0; i < size; i++)
	{
    
    
		remove(files[i].c_str());
	}
 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/gls_nuaa/article/details/129889476
今日推荐