C++复制所有指定后缀的文件

C++复制所有指定后缀的文件,

代码如下

#include <iostream>
#include <vector>
#include <io.h>
#include <fstream>
#include <string>
using namespace std;

vector<string> test;
const string dest_path("D:/code/lib/source");

bool suffix_is(char *s, char* suf)
{
    if(s[0] == '\0') return false;

    int s_len = strlen(s);
    char* dot_pos = s + (s_len - 1);
    while(  (*dot_pos != '.') && (dot_pos != s) ) --dot_pos;
    ++dot_pos;
    while ((*dot_pos)!='\0')
    {
        if(*suf == '\0') return false;
        if(*dot_pos != *suf) return false;
        ++dot_pos;
        ++suf;
    }

    return (*dot_pos == '\0');
}

int file_xcopy_stod(const char* source_dir, const char* destination_dir)
{
  	 ifstream infile(source_dir, ios::in | ios::binary);
	 if (infile.is_open() == 0) {
		 cout << "文件" << source_dir << "打开失败" << endl;
		 return -1;
	 }	
  	ofstream outfile(destination_dir, ios::out | ios::binary);
	 if (outfile.is_open() == 0) {
	 	 cout << "文件" << destination_dir << "打开失败" << endl;
		  infile.close();
 		 return -1;
	 }

	 const int FLUSH_NUM = 1024 * 1024;
	 char* ch = new(nothrow)char[FLUSH_NUM];
	 if (ch == NULL) {//出错处理
		  cout << "动态申请内存失败" << endl;
		  infile.close();//记得关闭
 		 outfile.close();//记得关闭
 		 return -1;
	 }
	 while (!infile.eof()) 
     {
		  infile.read(ch, FLUSH_NUM);
		  outfile.write(ch, infile.gcount());
	 }
	 delete[]ch;
	 infile.close();
	 outfile.close();
	 return 0;
} 

//遍历当前目录,不进入子文件夹
std::vector<std::string> getDirs(std::string dir) 
{
    std::vector<std::string> folders;
    intptr_t  hFile;
    struct _finddata_t fileinfo;
    hFile = _findfirst(dir.append("/*").c_str(), &fileinfo);
 
    //printf("%s\n", fileinfo.name);
    while (0 == _findnext(hFile, &fileinfo))
    {
        if (!(fileinfo.attrib & _A_SUBDIR))
        {
            if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                folders.push_back(fileinfo.name);
        }
    }
    _findclose(hFile);
    return folders;
}

//遍历当前目录及其所有子目录
void getAllFiles(std::string path, std::vector<std::string>& files)
{ 
    intptr_t   hFile = 0;  
    struct _finddata_t fileinfo;
    std::string p;
    if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);
            }
            else
            {
                if(suffix_is(fileinfo.name, "c\0") || suffix_is(fileinfo.name, "cc\0") || suffix_is(fileinfo.name, "cpp\0"))
                    files.push_back(p.assign(path).append("/").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return;
}

int main()
{
    getAllFiles("D:/code/include/absl",test);
    for(auto i = test.begin(); i!=test.end(); ++i)
    {
        //cout << *i <<'\n';
        string source_file = *i;
        auto index = source_file.find_last_of('/');
        string dest_file = dest_path;
        dest_file.append("/").append(source_file.substr(index + 1));
        cout << dest_file <<'\n';
        file_xcopy_stod( (*i).c_str(), dest_file.c_str());
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值