sprintf problem - urgent help needed!

Hi,

I have a problem with sprintf, using the GNU gcc compiler for 68k series processor.

I am trying to create a buffer of data, adding strings and variables to that buffer.

Look at the code (the buffer is unsigned char, 5120 bytes long).

int AddDataFunc1(unsigned char * ptrDataBuf)
{
int NewDataLen=0;

NewDataLen += sprintf((char*)(ptrDataBuf+NewDataLen),"%s %s %s,",String1,String2,String3);

NewDataLen += sprintf((char*)(ptrDataBuf+NewDataLen),"%1d,%1d,",Variable1,Variable2);

return NewDataLen;
}

I am calling this function like this:
NumBytesAdded += AddDataFunc1(&DataBuffer[NumBytesAdded]);
NumBytesAdded += AddDataFunc2(&DataBuffer[NumBytesAdded]);
and so on...

The problem is that sprintf ALWAYS returns a negative number (indicating it's failing somewhere) - but it works fine under MSVC, not the GNU compiler. I've tried looking everywhere on websites etc for help and can find nothing.

There is plenty of memory so it's not that, I am sure. I am running GCC libraries egcs-2.91.66

If anyone can help I will be eternally grateful.

Regards,
Eddie Byard

Comments

  • Works OK here, albeit for a different version of gcc on a different processor

    How about adding some debug?

    Have you tested the code in isolation, away from the rest of your code?

    [code]
    int AddDataFunc1( unsigned char * ptrDataBuf)
    {
    int NewDataLen=0;
    int n;

    n = sprintf((ptrDataBuf+NewDataLen),"%s %s %s,",String1,String2,String3);
    if ( n >= 0 ) {
    NewDataLen += n;
    n = sprintf((ptrDataBuf+NewDataLen),"%1d,%1d,",Variable1,Variable2);
    if ( n >= 0 ) {
    NewDataLen += n;
    } else {
    perror( "sprintf 2" );
    }
    } else {
    perror( "sprintf 1" );
    }
    return NewDataLen;
    }
    [/code]

  • http://www.gnu.org/prep/standards_29.html#SEC29

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • Thanks Darius...what a classic....

    "Don't use the return value of sprintf. It returns the number of characters written on some systems, but not on all systems."

    So great. I can't believe such a commonly used function would not do what the standard says....

    Cheers
    Eddie

    "Can we keep the earth from sinking?"
    - Too Much To Ask, Bad Religion

    : http://www.gnu.org/prep/standards_29.html#SEC29
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :

  • Standards compliant libraries -
    "Is it too much to ask?"
    : "Can we keep the earth from sinking?"
    : - Too Much To Ask, Bad Religion
    :
    : : http://www.gnu.org/prep/standards_29.html#SEC29
    : :
    : : "We can't do nothing and think someone else will make it right."
    : : -Kyoto Now, Bad Religion
    : :
    : :
    :
    :


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories