Thread: Replace strncpy with snprintf, how?

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    How about something like this:
    snprintf (dest, 10, "%.*s", 10, src);

    or maybe in this form:
    Code:
    char *newstrncpy (char *d, const char *s, int maxLen)
    {
       snprintf (d, maxLen, "%.*s", maxLen, s);
       return (d);
    }
    Then again, I think that with such format even basic sprintf would be fine:
    Code:
    char *newstrncpy (char *d, const char *s, int maxLen)
    {
       sprintf (d, "%.*s", maxLen, s);
       return (d);
    }

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ummm.... did you not read why we said we recommend not using s*printf()?

    Example:
    Code:
    char *strcpy_n(char *d, const char *src, size_t max)
    {
       register char *dst = d;
    
       if(dst && src && max)
          for(;*src && --max;)
             *dst++ = *src++;
       else
          return 0;
    
       *dst++ = 0;
       return d;
      
    }
    Jesus... If I wanted to use sprintf() I would have just used sprintf().

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    Quote Originally Posted by master5001 View Post
    Ummm.... did you not read why we said we recommend not using s*printf()?
    Yes, but original poster wanted sprintf() solution and I gave him. BTW, I use my own strNCpy() and it is very similar to yours. Maybe without that last increment and returning a useful address.

    Quote Originally Posted by master5001 View Post
    Jesus... If I wanted to use sprintf() I would have just used sprintf().
    Yes, but no one new how to use it properly
    Last edited by idelovski; 09-11-2008 at 01:50 AM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    strncpy is indeed not very good, because it will not put a zero at the end of the string, which is bad stuff if the string JUST fits.
    hmm... yes, that is kind of the answer I was looking for when I asked what was wrong with strncpy(), though my normal use of strncpy() does involve appending a null terminator manually - which is a problem, since people forget.

    But really "safestrncpy" should be the right solution. It should only be about 3-4 lines of actual code (not counting lines with braces etc).
    This would be my attempt to implement safestrncpy with strncpy:
    Code:
    char *safestrncpy(char *dest, const char *src, size_t n)
    {
        if (n != 0)
        {
            strncpy(dest, src, n - 1);
            dest[n - 1] = '\0';
        }
        return dest;
    }
    Unlike master5001's example, it returns the destination even is nothing is copied: returning a null pointer is probably wrong. However, unlike snprintf() and master5001's example, it may unnecessarily append null characters, since strncpy() does that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Ummm.... did you not read why we said we recommend not using s*printf()?

    Example:
    Code:
    char *strcpy_n(char *d, const char *src, size_t max)
    {
       register char *dst = d;
    
       if(dst && src && max)
          for(;*src && --max;)
             *dst++ = *src++;
       else
          return 0;
    
       *dst++ = 0;
       return d;
      
    }
    Jesus... If I wanted to use sprintf() I would have just used sprintf().
    A few comments (aside from the comment that Laserlight made about returning NULL):
    1. don't use "register". Modern compilers do that fine in themselves.
    2. Why use for, when while is what you are actually doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strncpy adavnced :P
    By Joke in forum C Programming
    Replies: 3
    Last Post: 07-14-2008, 11:14 AM
  2. Replace Array
    By SARAHCPS in forum C Programming
    Replies: 9
    Last Post: 11-15-2005, 11:07 AM
  3. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM
  4. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 03:05 PM