Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Download Text File into String Variable

Download a text file directly into a string variable.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkFtp2.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkFtp2 ftp;

    ftp.put_Hostname("ftp.someFtpServer.com");
    ftp.put_Username("myFtpUserAccount");
    ftp.put_Password("myFtpPassword");

    // Set other possible settings...
    // See http://www.cknotes.com/determining-ftp2-connection-settings/ 
    // for more information about FTP connection settings.
    ftp.put_Passive(true);
    ftp.put_AuthTls(true);

    // Connect and login to the FTP server.
    bool success = ftp.Connect();
    if (success != true) {
        strOut.append(ftp.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Change to the remote directory where the existing file is located.
    success = ftp.ChangeRemoteDir("junk");
    if (success != true) {
        strOut.append(ftp.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Download the contents of the remote file into a string variable.

    // Note: By setting the Utf8 property to true, we are telling the ftp object
    // to return all strings in the utf-8 encoding. The GetRemoteFileTextData method
    // downloads an ANSI text file.  However, because we've set the Utf8 property = true,
    // it is automatically converted and returned as utf-8.
    ftp.put_Utf8(true);

    // The GetRemoteFileTextData method assumes the remote file contains ANSI chars.
    // To download text files containing non-ANSI text, such as utf-8, call GetRemoteFileTextC
    // instead. (see below)
    const char *fileContents = ftp.getRemoteFileTextData("ansiText.txt");
    if (ftp.get_LastMethodSuccess() != true) {
        strOut.append(ftp.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }
    else {
        strOut.append(fileContents);
        strOut.append("\r\n");
    }

    // To download a remote text file containing utf-8 chars, 
    // call GetRemoteFileTextC and pass "utf-8" for the 2nd arg.  This tells
    // Chilkat to interpret the incoming bytes according to the utf-8 character encoding.

    fileContents = ftp.getRemoteFileTextC("utf8Text.txt","utf-8");
    if (ftp.get_LastMethodSuccess() != true) {
        strOut.append(ftp.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }
    else {
        strOut.append(fileContents);
        strOut.append("\r\n");
    }

    success = ftp.Disconnect();


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.