zoukankan      html  css  js  c++  java
  • C++ 文件复制

     1 #include<iostream>
     2 #include<fstream>
     3 
     4 void copy(char* src, char* dst);
     5 int main()
     6 {
     7     using namespace std;
     8     char src[50] = "E:/test/jdk-8u121-windows-x64.exe";
     9     char dst[50] = "E:\\test\\jdk-8u121-windows-x64_bak.exe";
    10     copy(src, dst);
    11     
    12     return 0;
    13 }
    14 
    15 void copy(char* src, char* dst)
    16 {
    17     using namespace std;
    18     ifstream in(src,ios::binary);
    19     ofstream out(dst,ios::binary);
    20     if (!in.is_open()) {
    21         cout << "error open file " << src << endl;
    22         exit(EXIT_FAILURE);
    23     }
    24     if (!out.is_open()) {
    25         cout << "error open file " << dst << endl;
    26         exit(EXIT_FAILURE);
    27     }
    28     if (src == dst) {
    29         cout << "the src file can't be same with dst file" << endl;
    30         exit(EXIT_FAILURE);
    31     }
    32     char buf[2048];
    33     long long totalBytes = 0;
    34     while(in)
    35     {
    36         //read从in流中读取2048字节,放入buf数组中,同时文件指针向后移动2048字节
    37         //若不足2048字节遇到文件结尾,则以实际提取字节读取。
    38         in.read(buf, 2048);    
    39         //gcount()用来提取读取的字节数,write将buf中的内容写入out流。
    40         out.write(buf, in.gcount());    
    41         totalBytes += in.gcount();
    42     }
    43     in.close();
    44     out.close();
    45 }
  • 相关阅读:
    负外边距--转载
    研究Dropbox Server端文件系统
    Bluetooth Profile for iPhone from the functional perspectives
    Somebody That I Used to Know
    复合查询
    聚合查询
    Filter查询
    ES基本查询
    ES版本控制
    ES基本操作
  • 原文地址:https://www.cnblogs.com/endenvor/p/6819043.html
Copyright © 2011-2022 走看看