how to convert a binary file to base64?

Discussion in 'C++' started by Cristiano, Mar 28, 2009.

  1. Cristiano

    Cristiano Guest

    Hello,

    I'm trying to send as an attachment a zip file, in my C++ application.

    To send using SMTP protocol, I'm trying to cripting the zip file to
    base64 code.

    I'm using this code to convert: http://www.adp-gmbh.ch/cpp/common/base64.html

    The code convert the zip file in base64 code and I can send the e-
    Mail.

    But, when I receive the e-Mail, I download the zip file and when I try
    to open it, I have a error mesage "the zip file is corrupted".

    I got this error just using zip files. Whan I attach text files, this
    erros doesn't exists.

    Please, can you help me with this?

    I'm converting the zip file like this:

    ============================================================
    string copyFileToString(string filename)
    {
    string retorno;

    FILE *from;

    from = fopen (filename.c_str(),"rb");

    char ch;
    /* copy the file */
    while(!feof(from))
    {
    ch = fgetc(from);
    if(ferror(from))
    {
    //printf("Error reading source file.\n");
    exit(1);
    }
    if(!feof(from))
    retorno += ch;
    }

    if(fclose(from)==EOF)
    {
    //printf("Error closing source file.\n");
    exit(1);
    }
    return retorno;
    }

    //...

    int main()
    {

    //...

    string file = copyFileToString("sample.zip");
    string fileBase64 = base64_encode(reinterpret_cast<const unsigned
    char*>(file.c_str()), file.length());

    //...

    }
    ============================================================

    So, am I doing some mistake ?

    Thank you.


    --
    Cristiano
     
    Cristiano, Mar 28, 2009
    #1
    1. Advertising

  2. Cristiano

    Kram Guest

    On Mar 28, 2:27 pm, Cristiano <> wrote:
    > Hello,
    >
    > I'm trying to send as an attachment a zip file, in my C++ application.
    >
    > To send using SMTP protocol, I'm trying to cripting the zip file to
    > base64 code.
    >
    > I'm using this code to convert:http://www.adp-gmbh.ch/cpp/common/base64.html
    >
    > The code convert the zip file in base64 code and I can send the e-
    > Mail.
    >
    > But, when I receive the e-Mail, I download the zip file and when I try
    > to open it, I have a error mesage "the zip file is corrupted".
    >
    > I got this error just using zip files. Whan I attach text files, this
    > erros doesn't exists.
    >
    > Please, can you help me with this?
    >
    > I'm converting the zip file like this:
    >
    > ============================================================
    > string copyFileToString(string filename)
    > {
    >   string retorno;
    >
    >   FILE *from;
    >
    >   from = fopen (filename.c_str(),"rb");
    >
    >   char ch;
    >   /* copy the file */
    >   while(!feof(from))
    >   {
    >     ch = fgetc(from);
    >     if(ferror(from))
    >     {
    >       //printf("Error reading source file.\n");
    >       exit(1);
    >     }
    >     if(!feof(from))
    >       retorno += ch;
    >   }
    >
    >   if(fclose(from)==EOF)
    >   {
    >     //printf("Error closing source file.\n");
    >     exit(1);
    >   }
    >   return retorno;
    >
    > }
    >
    > //...
    >
    > int main()
    > {
    >
    > //...
    >
    >     string file = copyFileToString("sample.zip");
    >     string fileBase64 = base64_encode(reinterpret_cast<const unsigned
    > char*>(file.c_str()), file.length());
    >
    > //...
    >
    > }
    >
    > ============================================================
    >
    > So, am I doing some mistake ?
    >
    > Thank you.
    >
    > --
    > Cristiano


    This forum is for questions about the C++ language and not about
    individual libraries, however I would think the error has to do with
    reading in the zip file as a text file instead of a binary file. Read
    the documentation on fopen, I think that will help.
     
    Kram, Mar 28, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren
    Replies:
    4
    Views:
    497
    Stefan Behnel
    Jul 13, 2006
  2. Harlin Seritt
    Replies:
    29
    Views:
    1,188
    Paul Rubin
    Feb 24, 2007
  3. DougJrs
    Replies:
    0
    Views:
    587
    DougJrs
    Apr 14, 2007
  4. Chunekit Pong

    Base64 string save to binary file

    Chunekit Pong, Nov 19, 2008, in forum: C++
    Replies:
    1
    Views:
    899
    Thomas J. Gritzan
    Nov 19, 2008
  5. phildog
    Replies:
    1
    Views:
    146
    phildog
    Jun 28, 2007
Loading...

Share This Page