Skip to main content
GameDev.net gamedev.net
🔒 Locked

Mail program

Started by H4ck3r Dec 31, 2002 at 10:36 AM 12 replies 250+ views
Original Post
H4ck3r
H4ck3r
Im building a mail program (visual C++). It works great, but now I have to add a attachment option. OK, First I open the file and read it. Then it uses Base64. The problem is; When I attach an exe file, only the first 3 bytes will be added. This is because the 4th character is nothing (chr(0), %00, 00h, u know?!). How can I solve this?
ga
ga
seems like you''re handling the files in text mode... do all operations in binary mode

GA
Visit our homepage: www.rarebyte.de.stGA
H4ck3r
H4ck3r
How do I do that??
ga
ga
if you''re using standard c file handling routines, using fopen(char *filename,char *openmode) to open/create files, add a ''b'' to the openmode string... do reads and writes with fread and fwrite..
if you don''t know how to use them, have a look at MSDN (i suppose you have got it with visual c++).

GA
Visit our homepage: www.rarebyte.de.stGA
CProgrammer
CProgrammer
Correct me if im mistaking, but I think this is all you need:

FILE *file = fopen(filename, "rb") //rb for read in binary mode

Edit: Yeah like ga said. We must have posted at the same time.

-CProgrammer

[edited by - CProgrammer on December 31, 2002 12:56:32 PM]
H4ck3r
H4ck3r
I do. Here''s my source to read the file:
FILE* file = fopen("F:\\Setup.exe", "rb");
if(file == NULL)
return 0;
fseek(file, 0, SEEK_END);
int nSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *pText = new char[nSize];

fgets(pText, nSize, file);
fclose(file);
Send(mail, (char*)Base64::Encode(pText).c_str());

The length of pText will become 3 with this code...
HellRaider
HellRaider
fgets() is used to read lines of text...

You should be using fread() instead... it''s binary safe, you pass it a file handle, a buffer (pText), an element size (sizeof(char)) and a length (nSize) and it will read the whole file to the buffer.
H4ck3r
H4ck3r
No, It''s just a home-made function...
H4ck3r
H4ck3r
Doesn't work. When I use:
int a = strlen(pText);

a will become 3. I don't understand it :S

[edited by - h4ck3r on January 1, 2003 7:08:14 AM]
barazor
barazor
to get the size of the file you will need to use fseek() with SEEK_END to go to the end, than use ftell() to give you the size
H4ck3r
H4ck3r
That''s what I am using. I now use this source:
FILE* file = fopen("F:\\Setup.exe", "rb");
if(file == NULL)
return 0;
fseek(file, 0, SEEK_END);
int nSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *pText = new char[nSize];

fread(pText, sizeof(char), nSize, file);
fclose(file);

And still, strlen(pText) = 3. Also when I use Base64::Encode(pText), the output becomes far to small for the size of setup.exe.
H4ck3r
H4ck3r
OK. Ive found out that the data is read without problems. The functions Base64::Encode stops with chr(0), %00, 00h, etc. This functions uses a std::string, so I think that's the problem... How can I fix THIS?!

Is there a base64 encode function without std::string?

[edited by - h4ck3r on January 1, 2003 8:23:01 AM]
Halo7
Halo7
The 4th character (0x00) is null terminating the string. strlen will look for that 0 byte to determine the end of string. I''m assuming that c_str is doing something similar.

For example, printf("foo\0bar") will only print foo because \0 terminates the string.

Try working with the data using functions that don''t deal with strings. Instead of storing it in a string, just allocate a buffer, pass buffer to Send function. Of course, doing this, you will have to store size of the buffer.

Halo7

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.