简体   繁体   中英

Copy a certificate file to another location

I have a .crl file which I want to copy to another location. From all the posts which I have seen till now, it can't be done without copying the contents. Is there any method in which I can transfer the file to another location in cpp without copying the contents?

I tried by copying the contents by using the usual fopen method. But the data was not being written to the buffer . If there is no direct method, could anyone please tell me how to read the certificate file and write the contents to another file in a different location?

I have also tried the fstream methods

std::ofstream dest("destination.crl", std::ios::trunc|std::ios::binary);

if(!dest.good())     
{         std::cerr << "error opening output file\n";      
          //std::exit(2);    
}    
std::fstream src("sourcec.crl", std::ios::binary); 
if(!src.good())     
{         std::cerr << "error opening input file\n"; 
          //std::exit(1);    
}
dest << src.rdbuf();   
if(!src.eof())           std::cerr << "reading from file failed\n";     
if(!dest.good())         std::cerr << "writing to file failed\n"; 

But it displayed the errors: error opening input file reading from file failed writing to file failed

Thank you in advance

I found this here .

See, this might help you. As you do not want to read, I thought you do not want to perform read and write operation yourself.

#include <istream>
#include <iostream>
#include <fstream>
#include <iterator>


using namespace std;


int main()

{

        fstream f("source.crl", fstream::in|fstream::binary);
        f << noskipws;
        istream_iterator<unsigned char> begin(f);
        istream_iterator<unsigned char> end;

        fstream f2("destination.crl",
        fstream::out|fstream::trunc|fstream::binary);
        ostream_iterator<char> begin2(f2);

        copy(begin, end, begin2);
}

Based on your response, I write another answer.

For Window, there is function [CopyFile][1] . You can use this function to copy the file without reading the content by yourself.

For linux/unix based, I am unable to find the direct equivalent of CopyFile .

I believe that this question might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM