How to copy a file in C/C++ with libssh and sftp

前端 未结 3 1625
迷失自我
迷失自我 2021-01-02 14:48

I\'m posting here because I need help with some code relating with libssh.

I read all the official documentation here but still I don\'t understand

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 15:34

    Open the file in the usual way (using C++'s fstream or C's stdio.h), read its contents to a buffer, and pass the buffer to sftp_write.

    Something like this:

    ifstream fin("file.doc", ios::binary);
    if (fin) {
      fin.seekg(0, ios::end);
      ios::pos_type bufsize = fin.tellg();   // get file size in bytes
      fin.seekg(0);                          // rewind to beginning of file
    
      std::vector buf(bufsize);        // allocate buffer
      fin.read(buf.data(), bufsize);         // read file contents into buffer
    
      sftp_write(file, buf.data(), bufsize); // write buffer to remote file
    }
    

    Note that this is a very simple implementation. You should probably open the remote file in append mode, then write the data in chunks instead of sending single huge blob of data.

提交回复
热议问题