zoukankan      html  css  js  c++  java
  • windows 与linux 下用C++读取sqlite实现文件复制(三)

    5.实现文件复制

     1 int CopyFile(char *SourceFile,char *NewFile)
     2 {
     3     ifstream in;
     4     ofstream out;
     5     in.open(SourceFile,ios::binary);//打开源文件
     6     if(in.fail())//打开源文件失败
     7     {
     8         cout<<"Error 1: Fail to open the source file."<<endl;
     9         in.close();
    10         out.close();
    11         return 0;
    12     }
    13     out.open(NewFile,ios::binary);//创建目标文件 
    14     if(out.fail())//创建文件失败
    15     {
    16         cout<<"Error 2: Fail to create the new file."<<endl;
    17         out.close();
    18         in.close();
    19         return 0;
    20     }
    21     else//复制文件
    22     {
    23         out<<in.rdbuf();
    24         out.close();
    25         in.close();
    26         return 1;
    27     }
    28 }

    linux c++ 拷贝文件问题

    自动包括子目录;
    通过命令行参数完成<[源路径]源文件><目的路径>
    要求源文件名支持通配符‘*’,例如:*.zip或*.rar,支持上述两种格式即可; 
    #include <stdlib.h>
    #include <string.h>
    #include <io.h>
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>
     1 /*********************************************************************
     2 功能:复制文件
     3 参数:pSrc,原文件名
     4 pDes,目标文件名
     5 返回:<0,失败
     6 >0,成功
     7 作者:
     8 *********************************************************************/
     9 #define BUF_SIZE    256
    10 int copyFile(const char * pSrc,const char *pDes)
    11 {
    12     FILE *in_file, *out_file;
    13     char data[BUF_SIZE];
    14     size_t bytes_in, bytes_out;
    15     long len = 0;
    16     if ( (in_file = fopen(pSrc, "rb")) == NULL )
    17     {
    18         perror(pSrc);
    19         return -2;
    20     }
    21     if ( (out_file = fopen(pDes, "wb")) == NULL )
    22     {
    23         perror(pDes);
    24         return -3;
    25     }
    26     while ( (bytes_in = fread(data, 1, BUF_SIZE, in_file)) > 0 )
    27     {
    28         bytes_out = fwrite(data, 1, bytes_in, out_file);
    29         if ( bytes_in != bytes_out )
    30         {
    31             perror("Fatal write error.
    ");
    32             return -4;
    33         }
    34         len += bytes_out;
    35         printf("copying file .... %d bytes copy
    ", len);
    36     }
    37     fclose(in_file);
    38     fclose(out_file);
    39     return 1;
    40 }
    41 
    42 /*********************************************************************
    43 功能:复制(非空)目录
    44 参数:pSrc,原目录名
    45 pDes,目标目录名
    46 返回:<0,失败
    47 >0,成功
    48 作者:
    49 *********************************************************************/
    50 int copyDir(const char * pSrc,const char *pDes)
    51 {
    52     if (NULL == pSrc || NULL == pDes) return -1;
    53     mkdir(pDes);
    54     char dir[MAX_PATH] = {0};
    55     char srcFileName[MAX_PATH] = {0};
    56     char desFileName[MAX_PATH] = {0};
    57     char *str = "\*.*";
    58     strcpy(dir,pSrc);
    59     strcat(dir,str);
    60     //首先查找dir中符合要求的文件
    61     long hFile;
    62     _finddata_t fileinfo;
    63     if ((hFile = _findfirst(dir,&fileinfo)) != -1)
    64     {
    65         do
    66         {
    67             strcpy(srcFileName,pSrc);
    68             strcat(srcFileName,"\");
    69             strcat(srcFileName,fileinfo.name);
    70             strcpy(desFileName,pDes);
    71             strcat(desFileName,"\");
    72             strcat(desFileName,fileinfo.name);
    73             //检查是不是目录
    74             //如果不是目录,则进行处理文件夹下面的文件
    75             if (!(fileinfo.attrib & _A_SUBDIR))
    76             {
    77                 copyFile(srcFileName,desFileName);
    78             }
    79             else//处理目录,递归调用
    80             {
    81                 if ( strcmp(fileinfo.name, "." ) != 0 && strcmp(fileinfo.name, ".." ) != 0 )
    82                 {
    83                     copyDir(srcFileName,desFileName);
    84                 }
    85             }
    86         } while (_findnext(hFile,&fileinfo) == 0);
    87         _findclose(hFile);
    88         return 1;
    89     }
    90     return -3;
    91 }
    View Code
  • 相关阅读:
    C#数组的Map、Filter、Reduce操作
    Safari中的input、textarea无法输入的问题
    小练手:用HTML5 Canvas绘制谢尔宾斯基三角形
    Web前端页面的浏览器兼容性测试心得(三)总结一些IE8兼容问题的解决方案
    大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画
    Web前端页面的浏览器兼容性测试心得(二)搭建原汁原味的IE8测试环境
    Web前端页面的浏览器兼容性测试心得(一)搭建测试用本地静态服务器
    CSS3、SVG、Canvas、WebGL动画精选整理
    利用JS代码快速获得知网论文作为参考文献的引用文本
    使用Node.js快速搭建简单的静态文件服务器
  • 原文地址:https://www.cnblogs.com/zxqdlenovo/p/4112069.html
Copyright © 2011-2022 走看看