sprintf()

This is a discussion on sprintf() within the C Programming forums, part of the General Programming Boards category; Hi, how can I add an int from in a loop repetetively , to an string by sprintf()....

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    sprintf()

    Hi,
    how can I add an int from in a loop repetetively , to an string by sprintf().

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sounds like a homework question...

    Hint:
    Code:
    char theString[256] = "This is the first part";
    sprintf(theString, "%s, %s" theString, "this is the second part");
    At the end of that, theString = "This is the first part, this is the second part". Now I'm sure you can figure out the rest

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you need to use the return value of sprintf http://linux.die.net/man/3/sprintf

    Code:
    char buf[BUF_SIZ];
    int len = 0;
    int ret = sprintf(buf,"%d ", 1);
    
    if(ret < 0)
    {
       perror("sprintf");
       return ret;
    }
    
    len += ret;
    
    ret = sprintf(buf+len, "%d ", 2);
    
    etc
    I hope you can figure out how to make it in the loop
    Last edited by vart; 08-14-2008 at 10:52 PM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > sprintf(theString, "&#37;s, %s" theString, "this is the second part");
    Bad idea.
    Like most C functions, this one is NOT safe when you use overlapping source and destination strings (in this case, theString).

    Sure, you may get lucky in this case, but try and put anything before the first %s, and you're in for a rude awakening.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Not to mention there's a missing comma.

    But you can use something like this . . .
    Code:
    sprintf(theString + strlen(theString), ", &#37;s", "this is the second part");
    You could even record the length of the string, since strlen() is a bit expensive to compute.
    Code:
    int len = 0;
    /* ... */
    while(condition) {
        len += sprintf(theString + len, ", %s", "this is the second part");
    }
    Note that this is ignoring the possibility of buffer overruns, which probably isn't the best idea . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    snprintf() is C99, and the OP said "sprintf". I was just suggesting that you could at least check to make sure len never goes above, say, 10 characters less than the maximum. (Assuming you're not adding more than 10 characters with each sprintf() call.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Oops my bad. One of those things I thought was right... thanks for pointing it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 09:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 03:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21