Sample code for 30+ languages & platforms
Unicode C

Check Whether the Last SFTP Read Failed

See more SFTP Examples

Demonstrates the Chilkat SFtp.LastReadFailed method, which returns whether the most recent read for a handle failed. The only argument is the handle. A normal end-of-file is not a failure.

Background: A chunked read loop needs to tell three outcomes apart: more data, a clean end-of-file, and an actual error such as a dropped connection or a revoked handle. LastReadFailed isolates the error case — it is false on a normal EOF read (which succeeds with zero bytes) and true for an empty, malformed, or already-closed handle — so the loop can stop cleanly on EOF but bail out with an error message on a genuine failure.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtpW sftp;
    int port;
    const wchar_t *password;
    const wchar_t *handle;
    BOOL reading;
    int chunkSize;
    HCkBinDataW bd;

    success = FALSE;

    //  Demonstrates the SFtp.LastReadFailed method, which returns whether the most recent read for a
    //  handle failed.  The only argument is the handle.  A normal end-of-file is NOT a failure.

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

    reading = TRUE;
    chunkSize = 4096;
    bd = CkBinDataW_Create();
    while (reading) {
        success = CkSFtpW_ReadFileBd(sftp,handle,chunkSize,bd);

        //  Distinguish an actual read failure from a normal EOF.  On EOF the read succeeds, returns
        //  zero bytes, and LastReadFailed is FALSE.
        if (CkSFtpW_LastReadFailed(sftp,handle)) {
            wprintf(L"Read failed: %s\n",CkSFtpW_lastErrorText(sftp));
            CkSFtpW_Dispose(sftp);
            CkBinDataW_Dispose(bd);
            return;
        }

        reading = !CkSFtpW_Eof(sftp,handle);
    }

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

    wprintf(L"Read %d bytes with no read failures.\n",CkBinDataW_getNumBytes(bd));

    CkSFtpW_Disconnect(sftp);


    CkSFtpW_Dispose(sftp);
    CkBinDataW_Dispose(bd);

    }