Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect, authenticate, and initialize the SFTP subsystem.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  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.s = "mySshPassword"

    success = CkSFtp::ckAuthenticatePw(sftp,"mySshLogin",password)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    handle.s = CkSFtp::ckOpenFile(sftp,"subdir/data.txt","readOnly","openExisting")
    If CkSFtp::ckLastMethodSuccess(sftp) = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  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.i = CkStringBuilder::ckCreate()
    If sbContent.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    chunkSize.i = 4096
    reading.i = 1
    While reading
        chunk.s = CkSFtp::ckReadFileText(sftp,handle,chunkSize,"utf-8")
        If CkSFtp::ckLastReadFailed(sftp,handle)
            Debug CkSFtp::ckLastErrorText(sftp)
            CkSFtp::ckDispose(sftp)
            CkStringBuilder::ckDispose(sbContent)
            ProcedureReturn
        EndIf

        CkStringBuilder::ckAppend(sbContent,chunk)
        reading = Not CkSFtp::ckEof(sftp,handle)
    Wend

    success = CkSFtp::ckCloseHandle(sftp,handle)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkStringBuilder::ckDispose(sbContent)
        ProcedureReturn
    EndIf

    Debug CkStringBuilder::ckGetAsString(sbContent)

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)
    CkStringBuilder::ckDispose(sbContent)


    ProcedureReturn
EndProcedure