Thread: Replace strncpy with snprintf, how?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    10

    Replace strncpy with snprintf, how?

    Gah! I've coded for about a week when I realize that on the project where I work, strncpy is forbidden. I must use snprintf instead. But I don't really understand how.

    For example:

    strncpy(dest,10,src);

    snprintf(dest,sizeof(dest),"%s",src);

    This copies the entire "src", how do I limit it to 10 characters?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %10s.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Nope. Doesn't work.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're right; I'm thinking minimum instead of maximum. snprintf takes a maximum as the second argument -- so presumably you can use 10 instead of sizeof(dest). (Why you know 10 is right instead of sizeof(dest) I don't know, but on your own head be it.)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    I can't do that either, the whole point of using snprintf is to write sizeof(dest) as the second argument, to be absolutely sure that I don't write beyond the limit of dest.

    10 is arbitrary, the point is "how to copy part of src", where 10 controls how much of the src I want in dest.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you realize how inconsistent you're being. Either you want to write *x* characters, or you want to write as many characters as dest holds. Pick one.

    (I suppose it's possible you want to write *x* characters unless that's too many: in that case use min(x,sizeof(dest)).)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've coded for about a week when I realize that on the project where I work, strncpy is forbidden. I must use snprintf instead.
    Why is strncpy() forbidden? Are you sure that it is not strcpy() that is forbidden?
    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

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    How about snprintf(dest, sizeof(dest) < 10 ? sizeof(dest) : 10, "&#37;s", src); ?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    I don't think you realize how inconsistent you're being. Either you want to write *x* characters, or you want to write as many characters as dest holds. Pick one.

    (I suppose it's possible you want to write *x* characters unless that's too many: in that case use min(x,sizeof(dest)).)
    I want to write x characters. More specifically, I have a very long string *from which* I want to write a shorter string, beeing x characters long, into a destination that normally can hold at least x characters.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Why is strncpy() forbidden? Are you sure that it is not strcpy() that is forbidden?
    strncpy (yes, WITH "n") is explicitly forbidden. I have no saying in that.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by OnionKnight View Post
    How about snprintf(dest, sizeof(dest) < 10 ? sizeof(dest) : 10, "%s", src); ?
    Maybe, I'll ask my superior if that's ok.

    Isn't this possible to do with the %s format string though?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by garton View Post
    I want to write x characters. More specifically, I have a very long string *from which* I want to write a shorter string, beeing x characters long, into a destination that normally can hold at least x characters.
    Then throw away sizeof(dest) and use 10, if that's the number of characters you want to write.

    If the problem is that you are getting the start of src that you don't want, then start copying from src[location].

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just write your own strncpy()? That should effectively make it fair game, right? Bloody teachers and their damn mind games.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Why not just write your own strncpy()? That should effectively make it fair game, right? Bloody teachers and their damn mind games.
    In this case, I'm not sure it's a teacher, but rather some sort of company policy. But if you really want to replace strncpy() with a better solution, why not write your OWN function, rather than using the MUCH more expensive snprintf(). snprintf() is probably about 6-10x slower than a simple strncpy(), because it really does complicated stuff to copy the string.

    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. snprintf() at least does that.

    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).

    --
    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.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes, corporate whorism. But yeah, that is why I too think writing your own function should suffice. snprintf() is just a buffer safe version of sprintf(). Which has a lot of overhead when one is just wanting to copy over a string.

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