By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

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

P: n/a
Do you have any answer to it? thx.

May 30 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Umesh wrote:
Do you have any answer to it? thx.
sed.

--
Ian Collins.
May 30 '07 #2

P: n/a
On May 30, 3:55 am, Umesh <fraternitydispo...@gmail.comwrote:
Do you have any answer to it? thx.

system("sed ...")

May 30 '07 #3

P: n/a
#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 <fraternitydispo...@gmail.comwrote:
Do you have any answer to it? thx.


system("sed ...")
May 30 '07 #4

P: n/a
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 <fraternitydispo...@gmail.comwrote:
>>Do you have any answer to it? thx.

system("sed ...")

--
Ian Collins.
May 30 '07 #5

P: n/a
On May 30, 9:33 am, Umesh <fraternitydispo...@gmail.comwrote:
#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 '07 #6

P: n/a
"Umesh" <fr****************@gmail.comschrieb im Newsbeitrag
news:11**********************@q19g2000prn.googlegr oups.com...
Quentin Godfroy wrote:
>On May 30, 3:55 am, Umesh <fraternitydispo...@gmail.comwrote:
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
May 30 '07 #7

P: n/a

"Umesh" <fr****************@gmail.comwrote:
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
May 30 '07 #8

P: n/a
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>
May 30 '07 #9

P: n/a
On May 30, 1:00 pm, Ian Collins <ian-n...@hotmail.comwrote:
Umesh wrote:
Do you have any answer to it? thx.

sed.

--
Ian Collins.
sed 's/foo/bar/g' <file_name>

Bye
Guru Jois

May 30 '07 #10

P: n/a
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:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
May 30 '07 #11

P: n/a
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
May 30 '07 #12

P: n/a
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

May 30 '07 #13

P: n/a
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.
May 30 '07 #14

P: n/a
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". :-)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 31 '07 #15

P: n/a
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.
May 31 '07 #16

P: n/a
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.
================================================== ===========
Jun 10 '07 #17

P: n/a
On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <st*****@swbell.net>
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
Jun 11 '07 #18

P: n/a
On Jun 11, 12:44 am, Barry Schwarz <schwa...@doezl.netwrote:
On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <stmi...@swbell.net>
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 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.