Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store binary chars into string value?

Status
Not open for further replies.

NicoPrenzel

Technical User
Aug 30, 2001
1
DE
Hello All,

this is my first post to this forum. Too, my C knowledge isn't really good! So my question could be hopefully easy to answer!

I call this function
PrintItemString(pPart, MimePartStruct.wByteCount, &mess );

where:
MIME_PART MimePartStruct;
char *pPart;
unsigned char *mess;
AND

int max_size = 250 * 1024;
if ((mess = malloc( max_size + 1 )) == NULL)
{
AddInLogMessageText( "Out of memory", NOERROR );
}

MIME_PART gives me a struct with description of length of binary part in pPart!

pPart does have binary characters (file) stored!

My aim is to store the binary characters (file) into mess.

The line
fprintf (dumpfile, "%c", *c);

gives me the right binary output into dumpfile!

But if i print out mess it gives me some wrong output!

Could someone please help?

Best regards!

NicoP.


void LNPUBLIC PrintItemString(char *pText, WORD iText, unsigned char **mess )
{
long int nChars = 0; /* How many characters have been written out? */

if (iText != 0)
{

while (iText--)
{
unsigned char *c = pText++;

fprintf (dumpfile, "%c", *c);

*mess = strcat( *mess, c );

nChars++;
}

}
return;
}
 
> *mess = strcat( *mess, c );
What are you hoping to achieve with this line?

If ptext was "hello", then all you seem to end up doing is appending this to mess
"hello"
"ello"
"llo"
"lo"
"o"
as you step through the string one char at a time

> fprintf (dumpfile, "%c", *c);
I would be suspicious of using this to print a binary file (it it truly is a binary file).
My first guess would be to think that this does NOT print \0 characters too well.

--
 
You should not use strcat. This only works for strings. That is it will stop coping when it finds a null (\0).

Your function should look as follows:

Code:
void    LNPUBLIC  PrintItemString(char *pText, WORD iText, unsigned char *mess )
{
    long int    nChars = 0;  /* How many characters have been written out? */
        
    if (iText != 0)
    {

      while (iText--)
      {    
          fprintf (dumpfile, "%c", *pText);

          *mess++ = *pText++;

          nChars++;
        }

   }
  return;
}

And it should be called:

Code:
PrintItemString(pPart,MimePartStruct.wByteCount,mess);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top