Sample code for 30+ languages & platforms
C

Read Bytes from a Remote SFTP File (BinData)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileBd method, which reads up to a number of bytes from the current position of an open handle and appends them to a BinData. The arguments are the handle, the maximum number of bytes, and the BinData. This example reads in chunks until end-of-file.

Background: For binary files, reading into a BinData preserves the bytes exactly, unlike a text read that decodes through a charset. Because it appends, the chunked read loop accumulates the whole file in the BinData; each pass reads up to the chunk size and the loop repeats until Eof. Fewer bytes than requested can come back near the end of the file, which is normal.

Chilkat C Downloads

C
#include <C_CkSFtp.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtp sftp;
    int port;
    const char *password;
    const char *handle;
    HCkBinData bd;
    int chunkSize;
    BOOL reading;

    success = FALSE;

    //  Demonstrates the SFtp.ReadFileBd method, which reads up to a number of bytes from the current
    //  position of an open handle and appends them to a BinData.  The 1st argument is the handle, the
    //  2nd is the maximum number of bytes, and the 3rd is the BinData.

    sftp = CkSFtp_Create();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    port = 22;
    success = CkSFtp_Connect(sftp,"sftp.example.com",port);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    password = "mySshPassword";

    success = CkSFtp_AuthenticatePw(sftp,"mySshLogin",password);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    success = CkSFtp_InitializeSftp(sftp);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    handle = CkSFtp_openFile(sftp,"subdir/image.png","readOnly","openExisting");
    if (CkSFtp_getLastMethodSuccess(sftp) == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    //  Read the file in chunks until end-of-file, accumulating the bytes in a BinData.
    bd = CkBinData_Create();
    chunkSize = 8192;
    reading = TRUE;
    while (reading) {
        success = CkSFtp_ReadFileBd(sftp,handle,chunkSize,bd);
        if (CkSFtp_LastReadFailed(sftp,handle)) {
            printf("%s\n",CkSFtp_lastErrorText(sftp));
            CkSFtp_Dispose(sftp);
            CkBinData_Dispose(bd);
            return;
        }

        reading = !CkSFtp_Eof(sftp,handle);
    }

    success = CkSFtp_CloseHandle(sftp,handle);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        CkBinData_Dispose(bd);
        return;
    }

    printf("Read %d bytes.\n",CkBinData_getNumBytes(bd));

    CkSFtp_Disconnect(sftp);


    CkSFtp_Dispose(sftp);
    CkBinData_Dispose(bd);

    }