代码很简单,调用了MFC里的几个函数。这里的冗余判断,是要遍历文件内容,进行两两比较。

需要注意的地方有两点:

1.源文件里头文件<afx.h>必须放在最前面。这里是为了避免nafxcwd.lib error LNK2005,由于CRT 库对 newdelete 和 DllMain 函数使用弱外部链接,MFC 库也包含 newdelete 和 DllMain 函数,这些函数要求先链接 MFC 库,然后再链接 CRT 库。

2.MFC库采取静态编译。这里是为了避免nafxcwd.lib error LNK2001。

代码实现:

 1 #include <afx.h>
 2 #include <iostream>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 vector<CFileStatus> cf;
 8 bool cmp(const CFileStatus &r1,const CFileStatus &r2){
 9     return r1.m_size<r2.m_size;
10 }
11 
12 int main(){
13     CFileFind finder;
14     CFile cfile;
15     CFileStatus rStatus;
16     BOOL bWorking=finder.FindFile(_T("*.txt"));
17 
18     cout<<"正在搜索当前目录下冗余文件..."<<endl;
19     while (bWorking){
20         bWorking = finder.FindNextFile();
21         CString fp = (LPCTSTR)finder.GetFilePath();
22         CFile::GetStatus(fp,rStatus);
23         cf.insert(cf.end(), rStatus);
24     }
25     sort(cf.begin(),cf.end(),cmp);
26     for(int i=0;i < cf.size(); i++){
27         int size = cf[i].m_size;
28         int num = 0;
29         bool tag = false;
30         CFile file1(cf[i].m_szFullName,CFile::modeRead);
31         char *FileContent1 = new char[cf[i].m_size];
32         file1.Read(FileContent1,cf[i].m_size);
33         for(int j=i+1;j < cf.size(); j++){
34             if(cf[i].m_size == cf[j].m_size){
35                 CFile file2(cf[j].m_szFullName,CFile::modeRead);
36                 char *FileContent2 = new char[cf[j].m_size];
37                 file2.Read(FileContent2,cf[j].m_size);
38                 for(num =0; num <size; num++){
39                     if(FileContent1[num] != FileContent2[num]){
40                         break;
41                     }
42                 }
43                 file2.Close();
44                 if(num == size){
45                     cout<<"找到一组冗余文件,正在删除其中冗余文件! "<<endl;
46                     file2.Remove(cf[j].m_szFullName);
47                     cf.erase(cf.begin()+j);
48                     j--;
49                     delete FileContent2;
50                 }
51             }else break;
52             file1.Close();
53         }
54     }
55     cout<<"已完成操作!"<<endl;
56     return 0;
57 }
View Code

相关文章:

  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
  • 2022-02-04
  • 2021-10-25
  • 2021-12-15
相关资源
相似解决方案