how to replace a string by other string in a text file?

Discussion in 'C Programming' started by Umesh, May 30, 2007.

  1. Umesh

    Umesh Guest

    Do you have any answer to it? thx.
    Umesh, May 30, 2007
    #1
    1. Advertising

  2. Umesh

    Ian Collins Guest

    Umesh wrote:
    > Do you have any answer to it? thx.
    >

    sed.

    --
    Ian Collins.
    Ian Collins, May 30, 2007
    #2
    1. Advertising

  3. On May 30, 3:55 am, Umesh <> wrote:
    > Do you have any answer to it? thx.



    system("sed ...")
    Quentin Godfroy, May 30, 2007
    #3
  4. Umesh

    Umesh Guest

    #include <stdio.h>
    #include <string.h>

    int main ()
    {
    char str[] ="This is a simple string";
    char * pch;
    pch = strstr (str,"simple");
    strncpy (pch,"sample",5);
    puts (str);
    return 0;
    }

    Quentin Godfroy wrote:
    > On May 30, 3:55 am, Umesh <> wrote:
    > > Do you have any answer to it? thx.

    >
    >
    > system("sed ...")
    Umesh, May 30, 2007
    #4
  5. Umesh

    Ian Collins Guest

    Umesh wrote:
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main ()
    > {
    > char str[] ="This is a simple string";
    > char * pch;
    > pch = strstr (str,"simple");
    > strncpy (pch,"sample",5);
    > puts (str);
    > return 0;
    > }
    >

    Will you ever learn not to top post?

    > Quentin Godfroy wrote:
    >> On May 30, 3:55 am, Umesh <> wrote:
    >>> Do you have any answer to it? thx.

    >>
    >> system("sed ...")

    >



    --
    Ian Collins.
    Ian Collins, May 30, 2007
    #5
  6. Umesh

    Guest

    On May 30, 9:33 am, Umesh <> wrote:
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main ()
    > {
    > char str[] ="This is a simple string";
    > char * pch;
    > pch = strstr (str,"simple");
    > strncpy (pch,"sample",5);
    > puts (str);
    > return 0;
    >
    > }


    Where's the text file your task definition specified?

    What happens when you want to replace "good" with "bad" or vice versa?
    , May 30, 2007
    #6
  7. "Umesh" <> schrieb im Newsbeitrag
    news:...
    > Quentin Godfroy wrote:
    >> On May 30, 3:55 am, Umesh <> wrote:
    >> > Do you have any answer to it? thx.

    >>
    >>
    >> system("sed ...")

    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main ()

    better: int main(void)

    > {
    > char str[] ="This is a simple string";
    > char * pch;
    > pch = strstr (str,"simple");

    strstr might return NULL (not in this specific sample though), so better
    check...

    > strncpy (pch,"sample",5);

    .... otherwise this might fail miserably then. It would fail too if there's
    not enough space left in str (i.e. if the replacement string is longer than
    the replaced), so better check that too

    if (pch && (strlen(pch) >= strlen("sample"))
    strncpy (pch,"sample",5);
    else {
    fprintf(stderr, "some error occured (check source for details)\n");
    return EXIT_FAILURE;
    }

    > puts (str);
    > return 0;
    > }


    There are more problems: you asked how to replace a string in a _text_file_,
    your code doesn't do that at all.
    Instead you are replacing _a_part_ of a string by another string,
    _in_memory_

    Well, at least now you made an efford to show some code, please continue
    this way. This plus not continuosly top posting plus not asking question in
    the Subject line only might lead to better answers.

    You're asking for help, so better comply with what the people willing to
    help would like your posts to look like...

    Bye, Jojo
    Joachim Schmitz, May 30, 2007
    #7
  8. "Umesh" <> wrote:

    > Do you have any answer to it? thx.


    42.

    ;-)

    Now, what was the question? Oh, yes, I see you put most of
    your message body in the "Subject" header. Ok.

    Replace a string in a file? Easy. There are several ways.
    Here's one way (known as the "slurp" method):

    1. Slurp the entire file into an array (or, preferably, a
    linked list) of strings.
    2. Using index or pointers, iterate to the string(s)
    you want to alter.
    3. Alter the string(s) you want to alter. Use the various
    functions in string.h to help you work with the strings.
    4. Output the altered list of strings, either to a new file,
    or to the same file (perhaps after making a *.bak copy of
    the original file).

    (Now, if you're expecting me to write you a whole program
    that does all that, fine... that'll be $100/hour, one-hour
    minimum. I take paypal.)

    --
    Cheers,
    Robbie Hatley
    East Tustin, CA, USA
    lonewolf aatt well dott com
    triple-dubya dott tustinfreezone dott org
    Robbie Hatley, May 30, 2007
    #8
  9. Umesh

    Tor Rustad Guest

    Umesh wrote:
    > Do you have any answer to it? thx.
    >


    Quit that C programming class, you don't have what it takes to become a
    C programmer anyway.


    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, May 30, 2007
    #9
  10. Umesh

    Guru Jois Guest

    On May 30, 1:00 pm, Ian Collins <> wrote:
    > Umesh wrote:
    > > Do you have any answer to it? thx.

    >
    > sed.
    >
    > --
    > Ian Collins.


    sed 's/foo/bar/g' <file_name>

    Bye
    Guru Jois
    Guru Jois, May 30, 2007
    #10
  11. Tor Rustad wrote:
    > Umesh wrote:
    >> Do you have any answer to it? thx.
    >>

    > Quit that C programming class, you don't have what it takes to become a
    > C programmer anyway.
    >

    Oh, I don't know. It is a truism repeated often enough the coders are lazy.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, May 30, 2007
    #11
  12. Umesh

    jacob navia Guest

    Clever Monkey wrote:
    > Tor Rustad wrote:
    >> Umesh wrote:
    >>> Do you have any answer to it? thx.
    >>>

    >> Quit that C programming class, you don't have what it takes to become
    >> a C programmer anyway.
    >>

    > Oh, I don't know. It is a truism repeated often enough the coders are
    > lazy.


    Yeah, repeated by many monkeys that think themselves clever
    but...

    Why generalizing from this lazy student asking for &a homework
    question to coders in general?

    Why is it that our profession is so looked down by most people that
    even in newsgroups that are meant for coders like this one we
    have to bear those stupid remarks?

    jacob
    jacob navia, May 30, 2007
    #12
  13. Umesh

    Chris Dollin Guest

    Clever Monkey wrote:

    > Tor Rustad wrote:
    >> Umesh wrote:
    >>> Do you have any answer to it? thx.
    >>>

    >> Quit that C programming class, you don't have what it takes to become a
    >> C programmer anyway.
    >>

    > Oh, I don't know. It is a truism repeated often enough the coders are lazy.


    Lazy, but not stupid. (And necessary, but not sufficient.)

    "If taking devious pains was a sign of laziness, he was prepared to
    call Hazelton the laziest man alive". (_Cities in Flight_, from
    memory and so likely not word-for-word.)

    --
    "Who do you serve, and who do you trust?" /Crusade/

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, May 30, 2007
    #13
  14. jacob navia said:

    > Clever Monkey wrote:
    >> Tor Rustad wrote:
    >>> Umesh wrote:
    >>>> Do you have any answer to it? thx.
    >>>>
    >>> Quit that C programming class, you don't have what it takes to
    >>> become a C programmer anyway.
    >>>

    >> Oh, I don't know. It is a truism repeated often enough the coders
    >> are lazy.

    >
    > Yeah, repeated by many monkeys that think themselves clever
    > but...


    You've misunderstood the irony in "Clever Monkey"'s reply.

    > Why generalizing from this lazy student asking for &a homework
    > question to coders in general?


    That's the irony. The thing is simply this: that the OP's laziness is
    the *wrong kind*, the kind that seeks other people who are willing to
    do one's work. The right kind of laziness is very different, and that's
    the kind to which programmers should aspire.

    >
    > Why is it that our profession is so looked down by most people that
    > even in newsgroups that are meant for coders like this one we
    > have to bear those stupid remarks?


    It isn't, and we don't, and they aren't - in that order.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 30, 2007
    #14
  15. CBFalconer said:

    > Richard Heathfield wrote:
    >> jacob navia said:
    >>

    > ... snip ...
    >>
    >>> Why is it that our profession is so looked down by most people
    >>> that even in newsgroups that are meant for coders like this one
    >>> we have to bear those stupid remarks?

    >>
    >> It isn't, and we don't, and they aren't - in that order.

    >
    > You omitted a terminal ", we don't". :)


    I don't think so - it's right there, in the middle. Email me if not
    entirely satisfied (hardly topical, after all) - see sig for address.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 31, 2007
    #15
  16. Umesh

    Stan Milam Guest

    Umesh wrote:
    > Do you have any answer to it? thx.
    >

    /**********************************************************************/
    /* File Name: replace.c. */
    /* Author: Stan Milam. */
    /* Date Written: 13-Jan-2003. */
    /* Description: */
    /* Implement a function replace all occurances of a pattern. */
    /* */
    /* (c) Copyright 2007 by Stan Milam. */
    /* All rights reserved. */
    /* */
    /**********************************************************************/

    #include <string.h>
    #include <stddef.h>
    #include "booldefs.h"

    /* $Revision: 1.3 $ */
    extern char tb_empty[];
    extern char tb_copyright[];
    static char *copyright = tb_copyright;

    /**********************************************************************/
    /* Name: */
    /* replace(). */
    /* */
    /* Synopsis: */
    /* #include "strtools.h" */
    /* char *replace(char *target, const char *from, char *to */
    /* */
    /* Description: */
    /* The replace function will replace all instances of the from */
    /* pattern with that of the to pattern. If the to pattern is */
    /* an empty string or NULL all instances of the from pattern will */
    /* be removed from the target string. */
    /* */
    /* Note: */
    /* Since a replacement pattern can be longer the the original */
    /* pattern the programmer must allow for enough memory to */
    /* accomodate a larger target string to allow successful */
    /* completion of the operation. */
    /* */
    /* Arguments: */
    /* char *target - The string in which patterns will be */
    /* replaced. Cannot be NULL */
    /* */
    /* const char *from - The pattern to be replaced. Cannot be NULL */
    /* or an empty string (i.e. ""). */
    /* */
    /* const char *to - The replacement pattern. Using a NULL */
    /* pointer will be interpreted the same as an */
    /* empty string (i.e. ""). */
    /* */
    /* Return Value: */
    /* In all cases the address of the target string. Should either */
    /* the target string or the from pattern be NULL the global errno */
    /* variable will be set to EINVAL. */
    /* */
    /**********************************************************************/

    char *
    replace ( char *p_target, const char *p_from, const char *p_to )
    {
    size_t to_len, from_len;
    char *l_to, *ptr, *rv = p_target;

    /**************************************************************/
    /* I should not have to do this but some compilers complain */
    /* when I try to assign the empty pointer to p_to. p_to */
    /* points to a string of const characters. I should be able */
    /* to change the pointer! */
    /**************************************************************/

    l_to = p_to == NULL ? tb_empty : (char *) p_to;

    /**************************************************************/
    /* Get the lengths before we enter the loop. */
    /**************************************************************/

    to_len = strlen( l_to );
    from_len = strlen( p_from );

    for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {

    /**********************************************************/
    /* We must either shrink out the replaced pattern or make */
    /* room for the new pattern. If the two patterns are of */
    /* equal length we don't care. */
    /**********************************************************/

    if ( to_len != from_len )
    memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);

    /**********************************************************/
    /* Once everthing is adjusted all we have to do is */
    /* replace :). */
    /**********************************************************/

    if ( to_len ) {
    memmove( ptr, l_to, to_len );
    ptr = ptr + to_len;
    }
    }

    return rv;
    }




    --
    Regards,
    Stan Milam
    =============================================================
    Charter Member of The Society for Mediocre Guitar Playing on
    Expensive Instruments, Ltd.
    =============================================================
    Stan Milam, Jun 10, 2007
    #16
  17. On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <>
    wrote:

    >Umesh wrote:
    >> Do you have any answer to it? thx.
    >>

    >/**********************************************************************/
    >/* File Name: replace.c. */
    >/* Author: Stan Milam. */
    >/* Date Written: 13-Jan-2003. */
    >/* Description: */
    >/* Implement a function replace all occurances of a pattern. */
    >/* */
    >/* (c) Copyright 2007 by Stan Milam. */
    >/* All rights reserved. */
    >/* */
    >/**********************************************************************/
    >
    >#include <string.h>
    >#include <stddef.h>
    >#include "booldefs.h"


    What is this?

    >
    >/* $Revision: 1.3 $ */
    >extern char tb_empty[];
    >extern char tb_copyright[];


    Where are these arrays supposed to be defined?

    >static char *copyright = tb_copyright;
    >
    >/**********************************************************************/
    >/* Name: */
    >/* replace(). */
    >/* */
    >/* Synopsis: */
    >/* #include "strtools.h" */
    >/* char *replace(char *target, const char *from, char *to */
    >/* */
    >/* Description: */
    >/* The replace function will replace all instances of the from */
    >/* pattern with that of the to pattern. If the to pattern is */
    >/* an empty string or NULL all instances of the from pattern will */
    >/* be removed from the target string. */
    >/* */
    >/* Note: */
    >/* Since a replacement pattern can be longer the the original */
    >/* pattern the programmer must allow for enough memory to */
    >/* accomodate a larger target string to allow successful */
    >/* completion of the operation. */
    >/* */
    >/* Arguments: */
    >/* char *target - The string in which patterns will be */
    >/* replaced. Cannot be NULL */
    >/* */
    >/* const char *from - The pattern to be replaced. Cannot be NULL */
    >/* or an empty string (i.e. ""). */
    >/* */
    >/* const char *to - The replacement pattern. Using a NULL */
    >/* pointer will be interpreted the same as an */
    >/* empty string (i.e. ""). */
    >/* */
    >/* Return Value: */
    >/* In all cases the address of the target string. Should either */
    >/* the target string or the from pattern be NULL the global errno */
    >/* variable will be set to EINVAL. */


    If you are going to set errno, don't you need to include errno.h?
    EINVAL is a non-standard value and the name is reserved for the
    implementation.

    >/* */
    >/**********************************************************************/
    >
    >char *
    >replace ( char *p_target, const char *p_from, const char *p_to )
    >{
    > size_t to_len, from_len;
    > char *l_to, *ptr, *rv = p_target;
    >
    > /**************************************************************/
    > /* I should not have to do this but some compilers complain */
    > /* when I try to assign the empty pointer to p_to. p_to */
    > /* points to a string of const characters. I should be able */
    > /* to change the pointer! */
    > /**************************************************************/
    >
    > l_to = p_to == NULL ? tb_empty : (char *) p_to;


    Why do you need l_to at all? If you want p_to to point to tb_empty,
    it should be declared const.

    >
    > /**************************************************************/
    > /* Get the lengths before we enter the loop. */
    > /**************************************************************/
    >
    > to_len = strlen( l_to );
    > from_len = strlen( p_from );
    >
    > for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {
    >
    > /**********************************************************/
    > /* We must either shrink out the replaced pattern or make */
    > /* room for the new pattern. If the two patterns are of */
    > /* equal length we don't care. */
    > /**********************************************************/
    >
    > if ( to_len != from_len )
    > memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);
    >
    > /**********************************************************/
    > /* Once everthing is adjusted all we have to do is */
    > /* replace :). */
    > /**********************************************************/
    >
    > if ( to_len ) {
    > memmove( ptr, l_to, to_len );
    > ptr = ptr + to_len;
    > }
    > }
    >
    > return rv;


    Why do you need rv at all? You can return p_target.

    >}


    You never set errno as promised in your documentation.


    Remove del for email
    Barry Schwarz, Jun 11, 2007
    #17
  18. Umesh

    Guest

    On Jun 11, 12:44 am, Barry Schwarz <> wrote:
    > On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <>
    > wrote:
    >
    >
    >
    >
    >
    > >Umesh wrote:
    > >> Do you have any answer to it? thx.

    >
    > >/**********************************************************************/
    > >/* File Name: replace.c. */
    > >/* Author: Stan Milam. */
    > >/* Date Written: 13-Jan-2003. */
    > >/* Description: */
    > >/* Implement a functionreplaceall occurances of a pattern. */
    > >/* */
    > >/* (c) Copyright 2007 by Stan Milam. */
    > >/* All rights reserved. */
    > >/* */
    > >/**********************************************************************/

    >
    > >#include <string.h>
    > >#include <stddef.h>
    > >#include "booldefs.h"

    >
    > What is this?
    >
    >
    >
    > >/* $Revision: 1.3 $ */
    > >extern char tb_empty[];
    > >extern char tb_copyright[];

    >
    > Where are these arrays supposed to be defined?
    >
    >
    >
    >
    >
    > >static char *copyright = tb_copyright;

    >
    > >/**********************************************************************/
    > >/* Name: */
    > >/* replace(). */
    > >/* */
    > >/* Synopsis: */
    > >/* #include "strtools.h" */
    > >/* char *replace(char *target, const char *from, char *to */
    > >/* */
    > >/* Description: */
    > >/* Thereplacefunction willreplaceall instances of the from */
    > >/* pattern with that of the to pattern. If the to pattern is */
    > >/* an empty string or NULL all instances of the from pattern will */
    > >/* be removed from the target string. */
    > >/* */
    > >/* Note: */
    > >/* Since a replacement pattern can be longer the the original */
    > >/* pattern the programmer must allow for enough memory to */
    > >/* accomodate a larger target string to allow successful */
    > >/* completion of the operation. */
    > >/* */
    > >/* Arguments: */
    > >/* char *target - The string in which patterns will be */
    > >/* replaced. Cannot be NULL */
    > >/* */
    > >/* const char *from - The pattern to be replaced. Cannot be NULL */
    > >/* or an empty string (i.e. ""). */
    > >/* */
    > >/* const char *to - The replacement pattern. Using a NULL */
    > >/* pointer will be interpreted the same as an */
    > >/* empty string (i.e. ""). */
    > >/* */
    > >/* Return Value: */
    > >/* In all cases the address of the target string. Should either */
    > >/* the target string or the from pattern be NULL the global errno */
    > >/* variable will be set to EINVAL. */

    >
    > If you are going to set errno, don't you need to include errno.h?
    > EINVAL is a non-standard value and the name is reserved for the
    > implementation.
    >
    >
    >
    >
    >
    > >/* */
    > >/**********************************************************************/

    >
    > >char *
    > >replace( char *p_target, const char *p_from, const char *p_to )
    > >{
    > > size_t to_len, from_len;
    > > char *l_to, *ptr, *rv = p_target;

    >
    > > /**************************************************************/
    > > /* I should not have to do this but some compilers complain */
    > > /* when I try to assign the empty pointer to p_to. p_to */
    > > /* points to a string of const characters. I should be able */
    > > /* to change the pointer! */
    > > /**************************************************************/

    >
    > > l_to = p_to == NULL ? tb_empty : (char *) p_to;

    >
    > Why do you need l_to at all? If you want p_to to point to tb_empty,
    > it should be declared const.
    >
    >
    >
    >
    >
    >
    >
    > > /**************************************************************/
    > > /* Get the lengths before we enter the loop. */
    > > /**************************************************************/

    >
    > > to_len = strlen( l_to );
    > > from_len = strlen( p_from );

    >
    > > for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {

    >
    > > /**********************************************************/
    > > /* We must either shrink out the replaced pattern or make */
    > > /* room for the new pattern. If the two patterns are of */
    > > /* equal length we don't care. */
    > > /**********************************************************/

    >
    > > if ( to_len != from_len )
    > > memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);

    >
    > > /**********************************************************/
    > > /* Once everthing is adjusted all we have to do is */
    > > /*replace:). */
    > > /**********************************************************/

    >
    > > if ( to_len ) {
    > > memmove( ptr, l_to, to_len );
    > > ptr = ptr + to_len;
    > > }
    > > }

    >
    > > return rv;

    >
    > Why do you need rv at all? You can return p_target.
    >
    > >}

    >
    > You never set errno as promised in your documentation.
    >
    > Remove del for email- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    My bad. I changed the code without changing the documentation. I've
    fixed it now. As to the other questions, well, I figured if the OP
    has any intelligence he can extrapolate from the example.

    Stan.
    , Jun 21, 2007
    #18
    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. Alun
    Replies:
    3
    Views:
    4,505
    Masudur
    Feb 18, 2008
  2. Sébastien Maurette
    Replies:
    3
    Views:
    139
    David Vallner
    Oct 2, 2006
  3. Prasad S
    Replies:
    2
    Views:
    229
    Dr John Stockton
    Aug 27, 2004
  4. juglesh
    Replies:
    16
    Views:
    325
    juglesh
    Dec 27, 2004
  5. mscir
    Replies:
    0
    Views:
    312
    mscir
    Oct 12, 2005
Loading...

Share This Page