Sample code for 30+ languages & platforms
PureBasic

Read Text from a Remote SFTP File (Sequential)

See more SFTP Examples

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

Background: Reading a file in bounded chunks keeps memory use predictable regardless of file size, which is why the loop reads a fixed amount and repeats until Eof. A subtlety worth knowing: the byte limit is applied to the encoded bytes, so a chunk boundary could in principle fall in the middle of a multibyte character — accumulating the pieces and decoding the whole avoids any such issue in practice here since the text is reassembled in a StringBuilder.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SFtp.ReadFileText method, which reads up to a number of bytes from the current
    ;  position of an open handle and decodes them to text.  The 1st argument is the handle, the 2nd
    ;  is the maximum number of bytes, and the 3rd is the charset.

    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

    ;  Open the remote file for reading.
    handle.s = CkSFtp::ckOpenFile(sftp,"subdir/notes.txt","readOnly","openExisting")
    If CkSFtp::ckLastMethodSuccess(sftp) = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Read the file in chunks until end-of-file, accumulating the text.
    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)

        ;  Stop once the end of the file has been reached.
        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