how to convert a binary file to base64?

C

Cristiano

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.
 
K

Kram

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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top