Sample code for 30+ languages & platforms
C

Set FTP Command and Directory-Listing Charsets

See more FTP Examples

Demonstrates the CommandCharset and DirListingCharset properties, which control how non-ASCII text is encoded in commands (including remote paths and filenames) and how directory-listing responses are decoded.

Background: FTP predates Unicode, so servers vary in how they encode filenames; getting non-ASCII names right means matching the server's encoding on both sides. CommandCharset (default ANSI) governs outgoing paths and filenames, while DirListingCharset governs decoding of LIST/NLST/MLSD replies. Modern servers typically use UTF-8 — often negotiated automatically via OPTS UTF8 ON — but setting these explicitly resolves mojibake with servers that do not advertise it.

Chilkat C Downloads

C
#include <C_CkFtp2.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2 ftp;
    const char *listing;

    success = FALSE;

    ftp = CkFtp2_Create();

    CkFtp2_putHostname(ftp,"ftp.example.com");
    CkFtp2_putUsername(ftp,"myFtpLogin");

    //  CommandCharset is the encoding used for commands containing non-ASCII text, including remote
    //  paths and filenames.  The default is ANSI; set utf-8 when the server uses UTF-8 filenames.
    CkFtp2_putCommandCharset(ftp,"utf-8");

    //  DirListingCharset is used only to decode directory-listing responses (LIST, NLST, MLSD).  It
    //  does not affect commands or paths.
    CkFtp2_putDirListingCharset(ftp,"utf-8");

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

    success = CkFtp2_Connect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    //  Non-ASCII remote paths and listings are now encoded/decoded as UTF-8.
    listing = CkFtp2_getTextDirListing(ftp,"");
    if (CkFtp2_getLastMethodSuccess(ftp) == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    printf("%s\n",listing);

    success = CkFtp2_Disconnect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }



    CkFtp2_Dispose(ftp);

    }