C++ 多线程拷贝文件(demo)

多线程拷贝文件

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

using namespace std;

//文件拷贝函数
int mycopy(string src,string des)
{
    try
    {
        ifstream file(src.c_str(),ios::in);
        if(!file.is_open()) 
        {
            cout << "open src error \n";
            return 1;
        }
        std::istreambuf_iterator<char> beg(file),end;
        string strdata(beg,end);
        file.close();

        std::ofstream fout(des.c_str(),ios::out);
        if (!fout.is_open())
        {
            cout <<"create "<" error.\n";
            return 1;
        }
        fout<std::flush;
        fout.close();
    }
    catch(exception e)
    {
        return 1;
    }
    return 0;
}
//任务信息
typedef struct TASK_INFO
{
    string src;
    string des;
}Info;

//获取目录下所有文件
int GetFileName(string strpath,vector<string>& vec)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp=opendir(strpath.c_str()))==NULL)
    {
        cout <<"Get File Name error.\n";
        return 1;
    }
    while((dirp=readdir(dp))!=NULL)
    {
        if( (strcmp(dirp->d_name,".")==0) || (strcmp(dirp->d_name,"..")==0))
            continue;
        vec.push_back(dirp->d_name);
        //cout << dirp->d_name<
    }
    return 0;
}
//组合src des路径名
int GetTaskInfo(vector<string>& vecfilename,string src,string des,vector& vecInfo)
{
    for(int i=0;i//cout <<"src:"<
        //cout <<"des:"<
    }
    return 0;
}
int npos=0;
pthread_mutex_t mut;//线程锁
//线程工作函数
void* task_cp(void *param)
{
    vector vec = *((vector*)param);
    for(;nposif(mycopy(info1.src,info1.des))
        {
            cout<" : copy error.\n";
        }
        else
        {
            cout<" : copy success.\n";
        }
        usleep(1);
    }

}
int main(int argc,char* argv[])
{
    //读取参数
    if(argc==2&&!strcmp(argv[1],"-h"))
    {
        cout<<"usage : mycp src des num\n";
        cout <<"src:"<<"源目录路径 /结尾";
        cout <<"des:"<<"目标路径 /结尾";
        cout <<"num:"<<"线程数量";
        return 0;
    }
    if(argc<4)
    {
        cout <<"paramer error\n";
        return 0;
    }
    int num_pthread=atoi(argv[3]);
    string src=argv[1];
    string des=argv[2];

    //获取文件名
    vector<string> vec;
    GetFileName(src,vec);

    //生成任务信息
    vector vecInfo;
    GetTaskInfo(vec,src,des,vecInfo);

    if(num_pthread>vecInfo.size())
        num_pthread=vecInfo.size();

    pthread_t pid[num_pthread];

    //开启多个线程
    for(int k=0;kvoid*)&vecInfo);
    }
    //等待所有线程工作结束退出
    for(int j=0;jreturn 0;
}

你可能感兴趣的:(C/C++)