libssh2_channel_read文件仅限最后一个字节

时间:2016-03-18 14:12:15

标签: c++ libssh2

我有一个远程大文件,只需要读取它的最后几个字节。 我怎样才能做到这一点? 我得到了整个文件的这个,但我没有看到我只能在不读完所有内容的情况下编写最后一个字节。

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);

1 个答案:

答案 0 :(得分:0)

好的,我使用libssh2_sftp_open而不是libssh2_sftp_seek64 感谢