Re: Adding characters in front of a string

Discussion in 'C Programming' started by Simon Biber, Aug 31, 2003.

  1. Simon Biber

    Simon Biber Guest

    "Martin" <> wrote:
    > Hi,
    >
    > Here is my question ?
    >
    > Users enter a search number in a form. Ex: 1906-02.
    >
    > What I need to do is to get this value and add some "0"
    > in FRONT of that string to obtain a string of 10 char.
    > Ex: 0001906-02.


    #include <string.h>

    void padZero(char *s)
    {
    size_t len = strlen(s);
    if(len < 10)
    {
    memmove(s + (10 - len), s, len + 1);
    memset(s, '0', 10 - len);
    }
    }

    --
    Simon.
     
    Simon Biber, Aug 31, 2003
    #1
    1. Advertising

  2. LibraryUser <> wrote in message news:<>...
    > Simon Biber wrote:
    > > "Martin" <> wrote:
    > > >
    > > > Users enter a search number in a form. Ex: 1906-02.
    > > >
    > > > What I need to do is to get this value and add some "0"
    > > > in FRONT of that string to obtain a string of 10 char.
    > > > Ex: 0001906-02.

    > >
    > > #include <string.h>
    > >
    > > void padZero(char *s)
    > > {
    > > size_t len = strlen(s);
    > > if(len < 10)
    > > {
    > > memmove(s + (10 - len), s, len + 1);
    > > memset(s, '0', 10 - len);
    > > }
    > > }

    >
    > Probably a safer mechanism, which takes into account the size of
    > the recipient storage, is to use strlcat and strlcpy (which are
    > non-standard, but available in source form in many places,
    > together with a definition).


    How is that going to be safer?

    Either s points to a buffer of sufficient size (11 bytes) or it
    doesn't. Using different utilities to perform the padding is not going
    to change that.

    --
    Peter
     
    Peter Nilsson, Sep 1, 2003
    #2
    1. Advertising

  3. (Peter Nilsson) wrote in
    <>:
    >
    >> Probably a safer mechanism, which takes into account the size of
    >> the recipient storage, is to use strlcat and strlcpy (which are
    >> non-standard, but available in source form in many places,
    >> together with a definition).

    >
    >How is that going to be safer?
    >
    >Either s points to a buffer of sufficient size (11 bytes) or it
    >doesn't. Using different utilities to perform the padding is not going
    >to change that.


    The strlcat()/strlcpy() functions take the size of the buffer as
    argument to prevent it from overflow, just like fgets(). So they
    are safe in the same way as fgets() is safe (compared to gets()).

    (Well, if the programmer provides an improper size argument, sth.
    is still unsafe, but it's not the function :))) )

    Irrwahn

    --
    Proofread carefully to see if you any words out.
     
    Irrwahn Grausewitz, Sep 1, 2003
    #3
  4. Simon Biber

    Simon Biber Guest

    "Irrwahn Grausewitz" <> wrote:
    > The strlcat()/strlcpy() functions take the size of the buffer as
    > argument to prevent it from overflow, just like fgets(). So they
    > are safe in the same way as fgets() is safe (compared to gets()).


    Not at all in the same way. The reason the gets/fgets issue happens
    is because the programmer has no control over how long a line will
    be entered. With strcat/strcpy, there is no such problem as the
    programmer has the knowledge of how long the source string is and
    how long the destination buffer is.

    --
    Simon.
     
    Simon Biber, Sep 1, 2003
    #4
  5. "Simon Biber" <> wrote in
    <3f5327c6$0$4186$>:

    >"Irrwahn Grausewitz" <> wrote:
    >> The strlcat()/strlcpy() functions take the size of the buffer as
    >> argument to prevent it from overflow, just like fgets(). So they
    >> are safe in the same way as fgets() is safe (compared to gets()).

    >
    >Not at all in the same way. The reason the gets/fgets issue happens
    >is because the programmer has no control over how long a line will
    >be entered. With strcat/strcpy, there is no such problem as the
    >programmer has the knowledge of how long the source string is and
    >how long the destination buffer is.


    True for the destination buffer (in fact it's a must for using
    strlc??()), but not necessarily for the source string, as this might
    have been dynamically allocated. Of course you can call strlen()
    to get the source's size, adding just another function call. But
    still the original strc??() functions will overflow your destination
    buffer if it's smaller than strlen( source_str ).

    Furthermore, one might think about using strncpy(), but this has
    two drawbacks:

    - you're not guaranteed to get a \0-terminated result;
    - \0-padding will occur, if the destination buffer is larger
    than the source.

    And if you're using strncat(), you always have to keep in mind not to
    take the space for the terminating \0 into account for the size
    argument!

    --
    I wish life had a scroll-back buffer.
     
    Irrwahn Grausewitz, Sep 1, 2003
    #5
  6. Irrwahn Grausewitz <> wrote in
    <>:

    <SNIP>
    >buffer if it's smaller than strlen( source_str ).

    ^^^^^^^^^^^^^^^^^^^^
    should read: strlen( source_str ) + 1

    --
    Proofread carefully to see if you any words out.
     
    Irrwahn Grausewitz, Sep 1, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RnJlZCBIZXJyaW5n?=

    Adding Front Page Extensions

    =?Utf-8?B?RnJlZCBIZXJyaW5n?=, Mar 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,227
    =?Utf-8?B?Qm9iIEMu?=
    Apr 27, 2006
  2. Dave Thompson

    Re: Adding characters in front of a string

    Dave Thompson, Sep 8, 2003, in forum: C Programming
    Replies:
    0
    Views:
    349
    Dave Thompson
    Sep 8, 2003
  3. Replies:
    4
    Views:
    88
    Phrogz
    Jul 27, 2007
  4. Replies:
    54
    Views:
    482
    Michele Dondi
    Jan 16, 2005
  5. Replies:
    8
    Views:
    190
Loading...

Share This Page