Thread: sprintf problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    sprintf problem

    Hi !

    Does sprintf have an buffer or something. I do the following :

    PrintINT( myFile, "TEST INT ", 4500 );
    PrintINT( myFile, "TEST INT ", 4501 );


    void PrintINT( FILE* f, char info[], int varde )
    {

    char sVarde[40];

    sprintf( sVarde, "%d\n", varde );
    strcat( info, " = ");
    strcat( info, sVarde);

    fwrite( info, strlen(info),1,f );


    }


    Result is :

    TEST INT = 4500

    = 4500
    = 4501

    I thougt it should be :-)

    TEST INT = 4500
    TEST INT = 4501


    Many thank's in advance!

    Regards
    Micael

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Found a way around

    I solved by :

    fprintf( f, "%s", info );
    fprintf( f, "%s", " = " );
    fprintf( f, "%d", varde );
    fprintf( f, "%s", "\n" );


    Regards
    Micael

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Hmm,

    Why you did it in 4 statemants?
    Make it in only one statement, you don't need the 4.

    Code:
    fprintf(f,"%s%s%d%s",info," = ",varde,"\n");

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The problem is you passed in "TEST INT " which should be treated as a constant string. But you tried modifying this string by using strcat and this could potentially cause problems because you're modifying memory you're not supposed to.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Smile Thanks

    Thanks for all your help !
    It's easy to be a beginner with a resource like this !!

    Regards
    Micael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM