Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用libcurl从FTP读取文件_C_Ftp_Libcurl - Fatal编程技术网

使用libcurl从FTP读取文件

使用libcurl从FTP读取文件,c,ftp,libcurl,C,Ftp,Libcurl,我正在使用libcurl编写一个从FTP服务器读取文件的应用程序。在阅读了关于这个库的所有内容后,我知道我只能将文件从FTP服务器下载到本地机器上,然后再阅读它们 我想知道libcurl中是否有任何定义,以便我只能从FTP服务器读取文件,因为我的本地设备中存储文件的空间非常有限 这是我到目前为止的做法 int establish_connection_to_ftp_server(char *uname_pass, char *url,char *filePath,char *err) { CUR

我正在使用
libcurl
编写一个从FTP服务器读取文件的应用程序。在阅读了关于这个库的所有内容后,我知道我只能将文件从FTP服务器下载到本地机器上,然后再阅读它们

我想知道libcurl中是否有任何定义,以便我只能从FTP服务器读取文件,因为我的本地设备中存储文件的空间非常有限

这是我到目前为止的做法

int establish_connection_to_ftp_server(char *uname_pass, char *url,char *filePath,char *err)
{
CURL *curl = NULL;
CURLcode res = -1;
FILE *ftpfile = NULL;
char error_disp[100] = {0};

ftpfile = fopen(filePath,"wb"); /* b is binary, needed on win32 */ 
if(ftpfile==NULL)
{
    printf("Unable to create file\n");
    return -1;
}

show_frame_progress("Establishing Connection to server...");
curl = curl_easy_init();
if(curl)
{
    curl_easy_setopt(curl, CURLOPT_USERPWD, uname_pass);
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func_upload);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300);

    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
}
fclose(ftpfile); /* close the local file */

if((int)res != 0)
{
    sprintf(error_disp,"Unable to connect to server ..\nCurl Error : %d \n%s",res,curl_easy_strerror(res));
    strcpy(err,error_disp);

    return -1;
}
return (int)res;
}

CURLOPT_WRITEFUNCTION
选项的默认值是
fwrite
(当然,它会将数据写入文件)。您可以使用与
fwrite
相同的签名创建自己的函数,并将其设置为该选项的值。然后libcurl将使用它下载的数据调用您的函数,您可以使用它执行任何操作