Download files from FTP (C ++, Win32)

Source: Internet
Author: User

// Testtmpmfc. cpp: defines the entry point of the console application.

# Include "stdafx. H"
# Include "testtmpmfc. H"
# Ifdef _ debug
# Define new debug_new
# Endif

# Include <string>
# Include <iostream>
# Include <afxinet. h>

# Include <windows. h>


// Unique application object

Cwinapp theapp;

Using namespace STD;

Int _ tmain (INT argc, tchar * argv [], tchar * envp [])
...{
// Test FTP
Int nretcode = 0;

// Initialize the MFC and display an error upon failure
If (! Afxwininit (: getmodulehandle (null), null,: getcommandline (), 0 ))
...{
// Todo: Change the error code to meet your needs.
_ Tprintf (_ T ("Fatal error: MFC initialization failed "));
Nretcode = 1;
}
Else
...{
// Todo: write code for the Application behavior here.
}

String serveripstr = "10.132.143.161 ";
String usrnamestr = "movie ";
String usrpwstr = "movie ";
String portstr = "21 ";
String serverfile = "/A/a.txt ";
String serverpath = "/";
String localfile = "./A/a.txt ";

Cinternetsession SESSION ("Update session ");
Cftpconnection * pftpconnection = NULL;
Try
...{
Pftpconnection = session. getftpconnection (serveripstr. c_str (), usrnamestr. c_str (), usrpwstr. c_str (), atoi (portstr. c_str ()));
If (pftpconnection = NULL)
Return false;
Bool B = pftpconnection-> setcurrentdirectory (serverpath. c_str ());
If (B = false)
...{
Afxmessagebox ("set current dir error ");
Return false;
}

// Create a directory
Createdirectory ("./A/", null );

// Check and delete the file
Win32_find_data findfiledata;
Handle hfind;

Hfind = findfirstfile (localfile. c_str (), & findfiledata );
If (hfind! = Invalid_handle_value)
...{
Deletefile (localfile. c_str ());
Cout <"find such a file" <Endl;
Findclose (hfind );
}

B = pftpconnection-> GetFile (serverfile. c_str (), localfile. c_str ());
If (B = false)
...{
Int error = getlasterror ();
Cstring strerror;
Strerror. Format ("download file error: % d", error );
Afxmessagebox (strerror );
Pftpconnection-> close ();
Return false;
}
Pftpconnection-> close ();
} Catch (cinternetexception * PEX )...{
Tchar SZ [1024];
Pex-& gt; geterrormessage (SZ, 1024 );
Printf ("error! % S ", SZ );
Pex-> Delete ();
Return false;
}

Return nretcode;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.