cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Certain filetypes recieved with libcurl are corrupted

From: Dario Griffo <dario.griffo.listas_at_gmail.com>
Date: Fri, 04 Jul 2008 16:54:22 -0300

Aaron M wrote:
> Hello! This seems like a very odd problem. All my code here is designed
> to do (for the moment), is to be passed a single flag from the
> commandline which dictates the name of the file to create. A fixed URL
> is accessed and it's contents are saved to that file. What's strange
> here though, is that specifically for doc files and pdf files, the
> resulting file is corrupt. It isn't empty, and even retains some of the
> file properties as the original (creator, keywords, etc), but for pdfs
> there is no text inside, and for docs, MSWord can not even open it. Does
> anything seem wrong in my code? Has anyone seen a simple example very
> similar, perhaps?
>
> http://myweb.unomaha.edu/~amills/datasheetCL.cpp

I suggets you change this
if (result == CURLE_OK)
        {
          file << buffer;
          exit(0);
        }

for something like this:
if (result == CURLE_OK)
        {
          file.write(buffer,totalReaded);
          exit(0);
        }

because operator << will truncate if found a '\0' and there is your error.
So, other recomendation is not to use a string, but a vector (parsing
headers, you know the filezise and can allocate the necessary memory).
or just open the stream and write on it in the callback function.
You can also open a FILE* and pass it to curl, and let it do the stuff...
Darío
Received on 2008-07-04