C++复制文件

C++复制文件

方式一

#include 
#include 
#define FLUSH_NUM 8

using namespace std;

int main(){

    ifstream in("/home/ClionProjects/mypro01/PAT/test", ios::binary);
    ofstream out("/home/ClionProjects/mypro01/PAT/test-copy", ios::binary);
    if (!in){
        printf("open file error");
        return -1;
    }
    if (!out){
        printf("open file error");
        return -1;
    }
    char flush[FLUSH_NUM];
    while(!in.eof()){
        in.read(flush, FLUSH_NUM);
        out.write(flush, in.gcount());
        printf("%d", in.gcount());
    }
    in.close();
    out.close();


    return 0;
}

方式二

#include 
#include 
#define FLUSH_NUM 8

using namespace std;

int main(){

    ifstream in("/home/ClionProjects/mypro01/PAT/test", ios::binary);
    ofstream out("/home/ClionProjects/mypro01/PAT/test-copy", ios::binary);
    if (!in){
        printf("open file error");
        return -1;
    }
    if (!out){
        printf("open file error");
        return -1;
    }
    out << in.rdbuf();
    in.close();
    out.close();
    return 0;
}

你可能感兴趣的:(C++)