C++實現文件拷貝的功能


#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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM