大致思路:
1. 得到路径内rgb/所有的照片名称,存入file容器里面
2. 遍历associate.txt 得到我们实际用的到的图片, 放入字符串str
3. 经过对比,删除掉file里面用得到照片,便可以得到没有用到的照片,相对于 求了一个用到的图片的补集
#include <iostream>
#include <fstream>
#include <cstdio>
#include <dirent.h>
#include <string.h>
#include <vector>
using namespace std;
void remove_img(string str);
void getFiles(std::string path, std::vector<std::string> &files);
int main()
{
ifstream infile;
infile.open("../associate.txt");
if (!infile.is_open())
{
cout<<"open file failed !"<<endl;
}
std::string path = "../rgb";
std::vector<std::string> files;
getFiles(path, files);
string s;
char img_path[30];
int flag =0,cnt=0;
while(!infile.eof())
{
getline(infile,s);
for (char ch :s)
{
if (ch == '/')
{
flag =1;
continue;
}
if (flag)
{
if (ch == ' ')
{
*(img_path+cnt) = '\0';
flag = 0;
break;
}
*(img_path+cnt) = ch;
cnt ++;
}
}
string str(img_path);
str ="../rgb/"+str;
cout<<str;
for (int i = 0; i < files.size(); ++i)
{
if (str.compare(files[i]) == 0)
{
files.erase(files.begin()+i);
break;
}
}
cnt =0;
cout<<endl;
}
for (int j = 0; j <files.size() ; ++j)
{
remove_img(files[j]);
}
return 0;
}
void getFiles(std::string path, std::vector<std::string> &files) {
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(path.c_str())) == NULL) {
perror("Open dir error...");
return;
}
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
continue;
else if (ptr->d_type == 8)
{
std::string strFile;
strFile = path;
strFile += "/";
strFile += ptr->d_name;
files.push_back(strFile);
} else {
continue;
}
}
closedir(dir);
}
void remove_img(string str)
{
const char *savePath = str.c_str();
if(remove(savePath)==0){
cout<<"删除成功"<<endl;
}
else{
cout<<"删除失败"<<endl;
}
printf("%s\n", savePath);
}