Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to convert a binary file to base64?

16 views
Skip to first unread message

Cristiano

unread,
Mar 28, 2009, 2:27:20 PM3/28/09
to
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

Kram

unread,
Mar 28, 2009, 4:58:55 PM3/28/09
to

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.

0 new messages