{\rtf1\ansi\ansicpg1252\deff0\deflang3081{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 int ftpGET(string local_url,string local_sub_dir,string filename, string rem_url,string rem_sub_dir, string REMOTE_UIDPWD);\par
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream);\par
\par
struct FtpFile \{\par
\tab   const char *filename;\par
  FILE *stream;\par
\};\par
\par
int main()\par
\{\par
\par
\tab string local_url,local_sub_dir;\tab\par
\tab string rem_url,rem_sub_dir;\par
\tab string REMOTE_UIDPWD;\par
\tab string filename;\par
\par
\tab local_url = "/home/abc";\par
\tab local_sub_dir = "";             // /myprog/ftp\par
\tab REMOTE_UIDPWD = "aaa\\\\bbb:ccc";\par
\tab filename = "/ftptest.txt";\par
\par
\tab rem_url = "ftp://remoteIP/FTPTest";\par
\tab rem_sub_dir = "/ftp/new";      \par
\tab int outget;\par
\tab outget = ftpGET(local_url,local_sub_dir,filename,rem_url,rem_sub_dir, REMOTE_UIDPWD);\par
\tab cout<<"outget :"<<outget<<endl;\par
\tab return 0;\par
\par
\}\par
\par
int ftpGET(string local_url,string local_sub_dir,string filename, string rem_url,string rem_sub_dir, string REMOTE_UIDPWD)\par
\{\par
  CURL *curl;\par
  CURLcode res;\par
  CURLcode result;\par
\tab string new_file;\par
int i_size;\par
i_size = filename.size();\par
cout<<"i_size :"<<i_size<<endl;\par
\par
new_file=filename.substr(1,i_size);\par
cout <<"new_file  :"<<new_file<<endl<<endl;\par
\par
  struct FtpFile ftpfile=\{\par
\tab     new_file.c_str(), /* name to store the file as if succesful */\par
    NULL\par
  \};\par
\par
\par
string LOCAL_FILE;\par
  LOCAL_FILE = local_url+local_sub_dir+filename;\par
  cout<<"LOCAL_FILE: "<<LOCAL_FILE<<endl;\par
  string REMOTE_URL;\par
  REMOTE_URL = rem_url+rem_sub_dir+filename;\par
  cout <<"REMOTE_URL: "<<REMOTE_URL<<endl;\par
\par
  curl_global_init(CURL_GLOBAL_DEFAULT);\par
\par
  curl = curl_easy_init();\par
  if(curl) \{\par
   \par
\tab\tab curl_easy_setopt(curl, CURLOPT_USERPWD, REMOTE_UIDPWD.c_str());\par
\tab\tab curl_easy_setopt(curl,CURLOPT_URL,REMOTE_URL.c_str());\par
\tab\tab  /* Define our callback to get called when there's data to be written */\par
\tab\tab  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);  // write_data\par
\par
    /* Set a pointer to our struct to pass to the callback */\par
\tab\tab  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);\par
\tab\par
\par
    /* Switch on full protocol/debug output */\par
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);\par
\par
    res = curl_easy_perform(curl);\par
\par
    /* always cleanup */\par
    curl_easy_cleanup(curl);\par
\par
    if( CURLE_OK == res )\par
\tab\tab\{\par
\tab\tab result=res;\par
\tab\tab\} else \par
\tab\tab\tab\{\par
\tab\tab\tab printf("ERROR NO  :%d\\n",res);\par
\tab\tab\tab printf("ERROR MSG :%s\\n",curl_easy_strerror( res ));\par
\tab\tab\tab result = res;\par
\tab\tab\tab\}\par
  \}\par
\par
  if(ftpfile.stream)\par
    fclose(ftpfile.stream); /* close the local file */\par
\par
\tab\par
  curl_global_cleanup();\par
\par
  return result;\par
\}\par
\par
\par
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)\par
\{\par
  struct FtpFile *out=(struct FtpFile *)stream;\par
  if(out && !out->stream)\par
\tab\{\par
    /* open file for writing */\par
    out->stream=fopen(out->filename, "wb");\par
    if(!out->stream)\par
     return -1; /* failure, can't open file to write */\par
\tab\}\par
 return fwrite(buffer, size, nmemb, out->stream);\par
\}\par
}
 