curl-library
help with this
Date: Sun, 12 Sep 2004 16:10:35 -0500
I have a problem with this code, it does the following:
Download the file archdone.lst by ftp and after it finished it opens the file and read it line by line.
The problem is that after finished downloading, i can't read the file. The ftp works fine but when i try to read the file is like it was and empty file, but in reality the transfer was ok.
Help with this please!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
struct FtpFile
{
char *filename;
FILE *stream;
};
int my_fwrite(void *buffer,size_t size,size_t nmemb,void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if (out && !out->stream)
{
out->stream=fopen(out->filename,"w");
if (!out->stream)
return -1;
}
return fwrite(buffer,size,nmemb,out->stream);
}
char *terminate(string,chars)
char *string;
char *chars;
{
char *ptr;
if (!string||!chars)
return "";
while (*chars)
{
if (ptr=strrchr(string,*chars))
*ptr='\0';
chars++;
}
return string;
}
int freadln(stream,lin)
FILE *stream;
char *lin;
{
char *p;
do
p=fgets(lin,355,stream);
while (p && (*lin=='#'));
if (!p)
return 1;
terminate(lin,"\n\r");
return 0;
}
int main(void)
{
FILE *flst;
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={"archdone.lst",NULL};
char buffer[35];
int num_partes;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl=curl_easy_init();
if (curl)
{
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,my_fwrite);
curl_easy_setopt(curl,CURLOPT_VERBOSE,0);
curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,60);
curl_easy_setopt(curl,CURLOPT_URL,"ftp://10.0.0.37/archdone.lst");
curl_easy_setopt(curl,CURLOPT_USERPWD,"userftp:user04");
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&ftpfile);
res=curl_easy_perform(curl);
if (CURLE_OK ==res)
{
flst=fopen("archdone.lst","r");
if (!flst)
{
printf("error: No se pudo abrir archdone.lst");
exit(1);
}
num_partes=0;
while (!freadln(flst,buffer))
{
printf("medio\n");
if (strncmp(buffer,"cdrdone",26)==0)
num_partes++;
}
fclose(flst);
}
}
if (ftpfile.stream)
fclose(ftpfile.stream);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
Received on 2004-09-12