473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Do you have any answer to it? thx.

May 30 '07 #1
18 2387
Umesh wrote:
Do you have any answer to it? thx.
sed.

--
Ian Collins.
May 30 '07 #2
On May 30, 3:55 am, Umesh <fraternitydispo...@gmail.comwrote:
Do you have any answer to it? thx.

system("sed ...")

May 30 '07 #3
#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
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
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
"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

"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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
4
by: Hardy Wang | last post by:
Hi: I have a XML like <?xml version="1.0" ?> <object> <comments>www.site.com/page.aspx?param1=value1&param2=value2</comments> </object> Since "&" is invalid in XML, I need to replace all "&"...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
23
by: dkirkdrei | last post by:
I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var =...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.