Sample code for 30+ languages & platforms
Unicode C

Upload Text to an FTP Server (StringBuilder)

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads it to a remote path. The arguments are the StringBuilder, the charset, whether to include a byte-order mark, and the remote file path.

Background: The text-oriented upload: assemble a document — CSV, config, JSON — in a StringBuilder and the method handles encoding as it writes. The charset governs the bytes actually stored, and the byte-order-mark flag is useful when a consuming Windows application expects a BOM.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;
    HCkStringBuilderW sb;
    BOOL includeBom;

    success = FALSE;

    //  Demonstrates the Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads
    //  it to a remote path.  The 1st argument is the StringBuilder, the 2nd is the charset, the 3rd
    //  selects whether a byte-order mark is included, and the 4th is the remote file path.

    ftp = CkFtp2W_Create();

    CkFtp2W_putHostname(ftp,L"ftp.example.com");
    CkFtp2W_putUsername(ftp,L"myFtpLogin");

    //  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.
    CkFtp2W_putPassword(ftp,L"myPassword");

    success = CkFtp2W_Connect(ftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    //  Build the text to upload.
    sb = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sb,L"name,quantity\n");
    CkStringBuilderW_Append(sb,L"widgets,42\n");

    //  Upload as UTF-8 without a byte-order mark.
    includeBom = FALSE;
    success = CkFtp2W_PutFileSb(ftp,sb,L"utf-8",includeBom,L"public_html/inventory.csv");
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        CkStringBuilderW_Dispose(sb);
        return;
    }

    wprintf(L"Uploaded text file.\n");

    success = CkFtp2W_Disconnect(ftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        CkStringBuilderW_Dispose(sb);
        return;
    }



    CkFtp2W_Dispose(ftp);
    CkStringBuilderW_Dispose(sb);

    }