带console进度条的文件拷贝程序,C++

来源:互联网 发布:mac mount o rw 编辑:程序博客网 时间:2024/05/17 06:41
#include <windows.h>
#include <stdio.h>
#include <fstream.h>

#define ERRCODE DWORD
#define BUFFERSIZE 36*1024

#define COPY_VERSION        "1.0"
#define err_OK              0
#define err_PARAMETER       1
#define err_OPENFILE        2
#define err_HANDLESCREEN    3
#define err_GETCURSOR       4

void ShowHelp(void);
void ShowUseage(void);
BOOL isFullPath(char* pszPath);
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName);
void ShowPer(DWORD per, DWORD filesize);
void ShowProcess(DWORD dwPer, DWORD filesize);

//useage:Copynew *** to ***
int main(int argc , char* argv[])
{
    //参数校验设计
    ERRCODE errCode = err_OK;
    switch(argc)
    {
        case 1:
            ShowHelp();
            return err_OK;
           
        case 2:
        if((strcmp(argv[1], "/?") == 0)
        || (strcmp(argv[1], "/h") == 0)
        || (strcmp(argv[1], "-h") == 0))
        {
            ShowHelp();
            errCode = err_OK;
            return errCode;
        }
        else
        {
            printf("Parameter error");
            ShowUseage();
            errCode = err_PARAMETER;
            return errCode;           
        }
       
        case 4:
        if(strcmp(argv[2],"to") != 0)
        {
            printf("Parameter error");
            ShowUseage();
            errCode = err_PARAMETER;
            return errCode;
        }
        break;
   
        default:
            printf("Parameter error");
            ShowUseage();
            errCode = err_PARAMETER;
            return errCode;
    }
   
    //功能代码设计
    char* pszSrcFileName;
    BOOL bIsFullPath;
    bIsFullPath = isFullPath(argv[1]);
    if (bIsFullPath)
    {
        pszSrcFileName = argv[1];
    }
    else
    {
        char szBuf[MAX_PATH];
        pszSrcFileName = szBuf;
        GetCurrentDirectory(MAX_PATH, pszSrcFileName);
        pszSrcFileName = StrCat_to_FullPath(pszSrcFileName, argv[1]);
    }
   
    char* pszDstFileName;
    bIsFullPath = isFullPath(argv[3]);
    if (bIsFullPath)
    {
        pszDstFileName = argv[3];
    }
    else
    {
        char szBuf[MAX_PATH];
        pszDstFileName = szBuf;
        GetCurrentDirectory(MAX_PATH, pszDstFileName);
        pszDstFileName = StrCat_to_FullPath(pszDstFileName, argv[3]);
    }   
   
    ifstream infile(pszSrcFileName, ios::binary);   
    if(infile.fail() == 1)
    {
        cout<<"can't open file "<<pszSrcFileName<<endl;
        errCode = err_OPENFILE;
        return errCode;
    }
    ofstream outfile(pszDstFileName, ios::binary);
    if(outfile.fail() == 1)
    {
        cout<<"can't open file "<<pszDstFileName<<endl;
        errCode = err_OPENFILE;
        return errCode;
    }
   
    HANDLE handleScreen;
    handleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
    if(!handleScreen)
    {
        return err_HANDLESCREEN;
    }
    COORD coordCursor;
    CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
    CONSOLE_CURSOR_INFO CursorInfo;
   
    char Buffer[BUFFERSIZE];
    HANDLE fSrcHandle;
    WIN32_FIND_DATA FileData1;
    fSrcHandle = FindFirstFile(pszSrcFileName,&FileData1);
    DWORD dwSrcFileSize = FileData1.nFileSizeLow;
    DWORD dwWrited = 0;
   
    printf("/n##################  File Copy Start !  ##################/n/n");

    do
    {   

        infile.read(Buffer,sizeof(char)*BUFFERSIZE);
        DWORD dwReaded=infile.gcount();
        outfile.write(Buffer,infile.gcount());
        dwWrited += dwReaded;
        ShowPer(dwWrited,dwSrcFileSize);
        ShowProcess(dwWrited,dwSrcFileSize);
       
        GetConsoleScreenBufferInfo(handleScreen, &ScreenBufferInfo);
        coordCursor = ScreenBufferInfo.dwCursorPosition;
        coordCursor.Y = coordCursor.Y-2;
        CursorInfo.dwSize = 66;
         CursorInfo.bVisible = FALSE;
        SetConsoleCursorInfo(handleScreen, &CursorInfo);
        SetConsoleCursorPosition(handleScreen,coordCursor);

    } while(!infile.eof());

    infile.close();
    outfile.close();
    printf("/n/n/n/n################# File Copy Completed ! #################/n/n");
    return err_OK;
}

//自定义函数行
void ShowHelp(void)
{
    printf(    "/n"
        "Copy file. v.%s/n"
        "Usage:/n"
        "    Copynew.exe srcFileName to dstFileName/n"
        "Copynew    [/? | /h | -h]/n"
        "/n"
        "/? /h -h    :               显示帮助信息./n"
        "srcFileName :               源文件的路径./n"
        "dstFileName :               目的文件路径./n"
        "/n",COPY_VERSION);
}

void ShowPer(DWORD dwPer, DWORD filesize)
{
    DWORD show = (DWORD)(100*((float)dwPer/filesize));
    printf("/r");
    printf("            %d%% of the file has been copied./n/n", show);
}

void ShowUseage(void)
{
    printf(    "/n"
            "Copy file.v%s./n"
            "Usage:/n"
            "    Copynew.exe srcFileName to dstFileName/n",COPY_VERSION);   
}

void ShowProcess(DWORD dwPer, DWORD filesize)
{
    printf(" $:==================================================:$");
    printf("/r");
    DWORD show = (DWORD)(100*((float)dwPer/filesize))/2;
    printf(" $:");
    for (DWORD i = 0; i<show ;i++)
    {
        printf(">");
    }
}

BOOL isFullPath(char* pszPath)
{   
    DWORD dwcount;
    dwcount = strlen(pszPath);
    for(DWORD dwi = 0; dwi < dwcount; dwi++)
    {
        if(*pszPath == ':')
            return TRUE;
        else
            pszPath ++ ;
    }
    return FALSE;
}

char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName)
{
    pszCurDirectory = strcat(pszCurDirectory,"//");
    pszCurDirectory = strcat(pszCurDirectory,pszFileName);
    return pszCurDirectory;
}
原创粉丝点击