Howdy, Stranger!

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

Categories

sprintf problem - urgent help needed!

ebyardebyard Member Posts: 2
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

  • SteveCSteveC Member Posts: 63
    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]

  • DariusDarius Member Posts: 1,666
    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

  • ebyardebyard Member Posts: 2
    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
    :
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
  • DariusDarius Member Posts: 1,666
    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

  • Justin BibJustin Bib USAMember Posts: 0

    _________ // http://forcoder.org // free ebooks and video tutorials about { Java Scratch C++ Assembly Ruby MATLAB Perl Go PL/SQL Visual Basic Visual Basic .NET PHP C Swift R Objective-C Delphi C# Python JavaScript Transact-SQL Apex D Scala Logo LabVIEW FoxPro Bash Scheme Prolog Kotlin VBScript ML SAS Lisp Rust Erlang COBOL Dart Alice Crystal Lua F# Hack ABAP Fortran Julia Ada Awk Clojure } ____________

Sign In or Register to comment.