2014-01-05 48 views
0

我正在使用libcurl來編寫從FTP服務器讀取文件的應用程序。閱讀了關於這個庫的所有信息之後,我才知道我只能從FTP服務器下載文件到本地機器,然後閱讀它們。使用libcurl從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; 
} 

回答

1

CURLOPT_WRITEFUNCTION選項的默認值是fwrite(其中,當然,將數據寫入文件)。您可以使用與fwrite相同的簽名創建自己的功能,並將其設置爲該選項的值。然後,libcurl會用它下載的數據調用你的函數,你可以隨心所欲地做任何事情。

相關問題