码迷,mamicode.com
首页 > 编程语言 > 详细

c++实现文件拷贝的功能

时间:2019-07-17 13:58:28      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:fst   文件输入   pat   ==   ssr   path   不为   har   sys   

#include<fstream.h>
#include<iostream.h>
#include<io.h>
#include<string>
#include<cstring>
#include<direct.h>
/*
* 路径转换,将单斜杠转换成双斜杠
*/
void getDouble(char * str, int len, char * temp) //
{
    char * start = NULL;
    char * t = NULL;
    start = str;
    t = temp;
    for(int i = 1; i <= len; i++, ++start) //循环len次,来处理‘\‘
    {
        if(* start == \\) //当为‘\‘时,在后面再加入一个‘\‘
        {
            * t = * start;
            ++t;
            * t = \\;
            ++t;
        }else{ //不为‘\‘时,原样复制到新空间
            * t = * start;
            ++t;
        }
    }
    * t = \0;
    //cout<<temp<<endl; //输出路径
}

/*
* 当有通配符*时,得到父路径
*/
void getParentPath(char * str, int len, char * temp) //得到父路径
{
    char * start = NULL;
    char * end = NULL;
    char * t = NULL;
    start = str;
    end = str + (len - 1); //指向最后一个位置
    t = temp;
    while( * end != \\){  //将end指向‘\‘
        end = end - 1;
    }
    ++end; //将指针放到‘\‘后面

    for(; start != end; start++) //将父路径写到temp中
    {
        * t = * start;
        ++t;
    }
    * t = \0; //加‘\0‘结束
    //cout<<"Parent Path:"<<temp<<endl;
}
/*
* 得到目的路径
*/
void getDesPath(char * des, char * desPath) //得到目的路径
{
    strcpy(desPath, (const char *)des);
    strcat(desPath, "\\\\");
    //cout<<"Des Path:"<<desPath<<endl;
}

void getCommend(char * p, char * src, char * des)
{
    strcpy(p, "xcopy ");
    strcat(p, (const char *)src);
    strcat(p, " ");
    strcat(p, (const char *)des);
    strcat(p, " /s/e");
    //cout<<"命令:"<<p<<endl;
}

void fileCopy(char * src, char * des){
    long lf; //定义打开文件的句柄
    _finddata_t file; //结构体,存储文件的信息
    char currentPath[100];
    char transSrcPath[100];
    char transDesPath[100];
    char desPath[100];
    unsigned char buf[100];
    
    if((lf = _findfirst((const char *)src, &file)) != -1L) //对c盘a文件夹进行复制
    {
            //cout<<"文件列表:"<<endl;
            do  //如果找到下个文件名字成功的话
            {    
            /*
                cout<<file.name<<endl;
                if(file.attrib == _A_NORMAL)
                    cout<<"普通文件"<<endl;
                else if(file.attrib == _A_RDONLY)
                    cout<<"只读文件"<<endl;
                else if(file.attrib == _A_HIDDEN)
                    cout<<"隐藏文件"<<endl;
                else if(file.attrib == _A_SYSTEM)
                    cout<<"系统文件"<<endl;
                else if(file.attrib == _A_SUBDIR)
                    cout<<"子目录"<<endl;
                else cout<<"存档文件"<<endl;
            */    
                getDouble(src, strlen((const char *)src), transSrcPath); //将转换的源路径存入transPath
                getParentPath(transSrcPath, strlen(transSrcPath), currentPath); //得到父路径 c:\\a\\
                
                getDouble(des, strlen((const char *)des), transDesPath); //将转换的目的路径存入transDesPath
                getDesPath(transDesPath, desPath); //得到目的路径 c:\\b\\

                
                    if(file.attrib == _A_SUBDIR){ //如果为子目录
                        /*
                        *    当为子目录的时候,利用系统的命令行参数
                        *    实现子目录以及子目录内文件的拷贝
                        */

                        char dirPath[100];
                        char cmd[100];
                        getParentPath(src, strlen((const char *)src), dirPath);
                        strcat(dirPath, file.name); //构建目录的源路径 
                        //cout<<"目录路径:"<<dirPath<<endl; // c:\a\bbbabc
                        getCommend(cmd, dirPath, des);
                        system((const char *)cmd); //调用系统的命令行参数实现文件夹的拷贝

                    }else{ //如果不是目录
                        /*
                        *    当文件不是目录时,利用fstream文件输入输出流来对每个文件
                        *    进行读/写操作,从而,达到复制的效果
                        */

                        ifstream fin((const char *)strcat(currentPath, file.name), ios::nocreate|ios::binary); //创建输入文件流
                        ofstream fout((const char *)strcat(desPath, file.name), ios::binary); //创建输入流
                
                        if(!fin){
                            cout<<"源文件路径没有找到!"<<endl;
                            return;
                        }
                        if(!fout){
                            cout<<"目的路径错误!"<<endl;
                            return;
                        }
                        while(!fin.eof()){ //实现文件的复制
                            fin.read(buf, sizeof(buf));        
                            fout.write(buf, fin.gcount());
                        }
                        fin.close(); //关闭流
                        fout.close();
                    }
                    
            }while(_findnext(lf, &file) == 0);
            cout<<"复制已完成!"<<endl;
            _findclose(lf);
        }else{
            cout<<"源文件路径没有找到!"<<endl;
        }
}

int main()
{
    char src[100], des[100]; 
    cout<<"请输入路径和源文件名称:"<<endl;
    cin>>src; // c:\\a\\*abc.txt
    cout<<"请输入目的路径:"<<endl;
    cin>>des;
    fileCopy(src, des); //调用文件拷贝函数

    return 0;
}

 

c++实现文件拷贝的功能

标签:fst   文件输入   pat   ==   ssr   path   不为   har   sys   

原文地址:https://www.cnblogs.com/mathyk/p/11200556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!