是使用curl CURLOPT_DIRLISTONLY时在ftp中的文件或目录

时间:2014-01-07 12:29:08

标签: c curl

我有功能,打印出ftp服务器上的文件列表。但我想知道它是目录还是文件。如何解决这个问题?

struct FtpFile {
  const char *filename;
  FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{


  struct FtpFile *out=(struct FtpFile *)stream;
char *p =buffer;
printf(p);
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  return fwrite(buffer, size, nmemb, out->stream);
}

bool GetFilesList()
{
    CURL *curl;
    CURLcode res;


    struct FtpFile ftpfile={"tmp.txt", /* name to store the file as if succesful */ NULL };




    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "ftp://sample.com/updFile1.txt");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
        curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
    }



    if(CURLE_OK != res)
        return false;

    return true;
}

0 个答案:

没有答案