Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Adding characters in front of a string

Reply
Thread Tools

Re: Adding characters in front of a string

 
 
Simon Biber
Guest
Posts: n/a
 
      08-31-2003
"Martin" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      09-01-2003
LibraryUser <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Simon Biber wrote:
> > "Martin" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-01-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Peter Nilsson) wrote in
<(E-Mail Removed) >:
>
>> 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.
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      09-01-2003
"Irrwahn Grausewitz" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-01-2003
"Simon Biber" <(E-Mail Removed)> wrote in
<3f5327c6$0$4186$(E-Mail Removed)>:

>"Irrwahn Grausewitz" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-01-2003
Irrwahn Grausewitz <(E-Mail Removed)> wrote in
<(E-Mail Removed)>:

<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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove special characters in front of the file names wong_powah@yahoo.ca Perl Misc 8 01-09-2008 08:29 PM
Triming characters from the front of a String... sketchitup@gmail.com Ruby 4 07-27-2007 03:56 PM
Adding a delimiter inbetween number characters and letter characters toomanyjoes@mail.utexas.edu Perl Misc 54 01-16-2005 04:07 PM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM
Re: Adding characters in front of a string Dave Thompson C Programming 0 09-08-2003 12:22 AM



Advertisments