简体   繁体   English

如何使用libcurl打印远程FTP目录列表?

[英]How do I use libcurl to printf a remote FTP directory listing?

I think I'm supposed to use CURLOPT_DIRLISTONLY but I'm not really sure how to proceed. 我认为应该使用CURLOPT_DIRLISTONLY但我不确定如何继续。

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DIRLISTONLY, long listonly);

I'm not sure how to make the call to actually print the list. 我不确定如何拨打电话以实际打印列表。

Enabling the CURLOPT_DIRLISTONLY option for FTP has the effect of retrieving only file names instead of file details (eg file size, date, etc.) basically as the difference between using a plain ls (only names) or a ls -l (listing with details) on UNIX. 启用FTP的CURLOPT_DIRLISTONLY选项的效果是,仅检索文件名而不是文件详细信息(例如文件大小,日期等),基本上是使用纯ls (仅名称)或ls -l (列出详细信息)之间的区别)。 At the FTP protocol level, enabling CURLOPT_DIRLISTONLY will make libcurl issue a NLST command rather than a LIST . 在FTP协议级别,启用CURLOPT_DIRLISTONLY将使libcurl发出NLST命令而不是LIST Then to get the directory listing you do the same as a file transfer, with the difference that your ftp:// URL will point to a directory (and not to a file). 然后,要获取目录列表,请执行与文件传输相同的操作,不同之处在于ftp:// URL将指向目录(而不是文件)。 The contents of that transfer will be your directory listing. 传输的内容将是您的目录列表。

To display the directory listing, rather than saving it to a file, you can use the CURLOPT_WRITEFUNCTION option to have libcurl call a callback function you supply for every block of data. 要显示目录列表,而不是将其保存到文件中,可以使用CURLOPT_WRITEFUNCTION选项使libcurl调用为每个数据块提供的回调函数。 That function only needs to fwrite the data to stdout to display it. 这个功能只需要fwrite的数据stdout来显示它。 It should look something like this: 它看起来应该像这样:

void write_callback(void* data, size_t size, size_t nmemb, void* ptr)
{
  fwrite(data, size, nmemb, stdout);
}

int main()
{
  // ...
  curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
  // ...
  ret = curl_easy_perform(handle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM