Sample code for 30+ languages & platforms
C

Open a Remote File on an SFTP Server

See more SFTP Examples

Demonstrates the Chilkat SFtp.OpenFile method, which opens or creates a remote file and returns a handle for subsequent read or write operations. The arguments are the remote path, the access mode (readOnly, writeOnly, or readWrite), and the create disposition. Close the handle with CloseHandle when finished.

Background: Handle-based access is the foundation for everything the by-name convenience methods hide. The create disposition is the key control — it decides whether a missing file is an error (openExisting), whether an existing file is truncated (createTruncate) or preserved (openOrCreate), and can add options such as appendData for logging. Because the handle names a server-side resource, it must be closed to avoid exhausting the server's open-file limit.

Chilkat C Downloads

C
#include <C_CkSFtp.h>

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

    success = FALSE;

    //  Demonstrates the SFtp.OpenFile method, which opens or creates a remote file and returns a
    //  handle for subsequent read or write operations.  The 1st argument is the remote path, the 2nd
    //  is the access mode, and the 3rd is the create disposition.

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

    //  access must be "readOnly", "writeOnly", or "readWrite".
    //  
    //  createDisposition is a comma-separated list containing exactly one primary disposition:
    //    createNew          Create a new file; fail if it already exists.
    //    createTruncate     Create a new file, or open and truncate an existing file.
    //    openExisting       Open an existing file; fail if it does not exist.
    //    openOrCreate       Open the file if it exists, otherwise create it.
    //    truncateExisting   Open and truncate an existing file; fail if it does not exist.
    //  Optional additional keywords include appendData, textMode, blockRead, blockWrite, and others.

    //  Open a remote file for reading.
    handle = CkSFtp_openFile(sftp,"subdir/data.txt","readOnly","openExisting");
    if (CkSFtp_getLastMethodSuccess(sftp) == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    printf("Opened file, handle = %s\n",handle);

    //  ... read from the handle ...

    //  Always close the handle when finished.
    success = CkSFtp_CloseHandle(sftp,handle);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    CkSFtp_Disconnect(sftp);


    CkSFtp_Dispose(sftp);

    }