Sample code for 30+ languages & platforms
Unicode C

Detect End-of-File on a Remote SFTP File

See more SFTP Examples

Demonstrates the Chilkat SFtp.Eof method, which returns whether the most recent read for a handle received the SFTP end-of-file status. The only argument is the handle. This example reads until Eof is true.

Background: The timing here is worth understanding: reading exactly through the final byte does not immediately set EOF. The next read succeeds, returns zero bytes, sets the status to SSH_FX_EOF, and only then does Eof report true. That is why the loop tests Eof after each read rather than assuming a short read means the end — a short read can also just be the server returning less than requested.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSFtpW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtpW sftp;
    int port;
    const wchar_t *password;
    const wchar_t *handle;
    HCkStringBuilderW sbContent;
    int chunkSize;
    BOOL reading;
    const wchar_t *chunk;

    success = FALSE;

    //  Demonstrates the SFtp.Eof method, which returns whether the most recent read for a handle
    //  received the SFTP end-of-file status.  The only argument is the handle.

    sftp = CkSFtpW_Create();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    port = 22;
    success = CkSFtpW_Connect(sftp,L"sftp.example.com",port);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_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 = L"mySshPassword";

    success = CkSFtpW_AuthenticatePw(sftp,L"mySshLogin",password);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    success = CkSFtpW_InitializeSftp(sftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    handle = CkSFtpW_openFile(sftp,L"subdir/data.txt",L"readOnly",L"openExisting");
    if (CkSFtpW_getLastMethodSuccess(sftp) == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    //  Read until Eof reports the end of the file.  Note that reading exactly through the final byte
    //  does not set EOF; the next read returns zero bytes and then Eof becomes true.
    sbContent = CkStringBuilderW_Create();
    chunkSize = 4096;
    reading = TRUE;
    while (reading) {
        chunk = CkSFtpW_readFileText(sftp,handle,chunkSize,L"utf-8");
        if (CkSFtpW_LastReadFailed(sftp,handle)) {
            wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
            CkSFtpW_Dispose(sftp);
            CkStringBuilderW_Dispose(sbContent);
            return;
        }

        CkStringBuilderW_Append(sbContent,chunk);
        reading = !CkSFtpW_Eof(sftp,handle);
    }

    success = CkSFtpW_CloseHandle(sftp,handle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        CkStringBuilderW_Dispose(sbContent);
        return;
    }

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbContent));

    CkSFtpW_Disconnect(sftp);


    CkSFtpW_Dispose(sftp);
    CkStringBuilderW_Dispose(sbContent);

    }