1

I have an remote big file, and only need to read the last bytes of it. How can I do this? I got this for the whole file, but I don't see where I can only write the last bytes without reading it all.

channel = libssh2_scp_recv2(session, remotefile.c_str(), &fileinfo);

    if (!channel) {
        fprintf(stderr, "Unable to open a session: %d\n",
                libssh2_session_last_errno(session));
        return;
    }


   FILE* pFile = fopen (filename.c_str(), "wb");

    while(got < fileinfo.st_size) {
        char mem[1024];
        int amount=sizeof(mem);

        if((fileinfo.st_size -got) < amount) {
            amount = (int)(fileinfo.st_size -got);
        }

        rc = libssh2_channel_read(channel, mem, amount);
        if(rc > 0) {
          fwrite (mem , sizeof(char), rc, pFile);
            //write(1, mem, rc);
        }
        else if(rc < 0) {
            fprintf(stderr, "libssh2_channel_read() failed: %d\n", rc);
            break;
        }
        got += rc;
    }
    fclose (pFile);

    libssh2_channel_free(channel);
superbem
  • 441
  • 3
  • 10

1 Answers1

0

Ok I managed using libssh2_sftp_open instead with libssh2_sftp_seek64 Thanks

superbem
  • 441
  • 3
  • 10