Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to replace a string by other string in a text file?

Reply
Thread Tools

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

 
 
Umesh
Guest
Posts: n/a
 
      05-30-2007
Do you have any answer to it? thx.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-30-2007
Umesh wrote:
> Do you have any answer to it? thx.
>

sed.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Quentin Godfroy
Guest
Posts: n/a
 
      05-30-2007
On May 30, 3:55 am, Umesh <(E-Mail Removed)> wrote:
> Do you have any answer to it? thx.



system("sed ...")

 
Reply With Quote
 
Umesh
Guest
Posts: n/a
 
      05-30-2007
#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 <(E-Mail Removed)> wrote:
> > Do you have any answer to it? thx.

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


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-30-2007
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 <(E-Mail Removed)> wrote:
>>> Do you have any answer to it? thx.

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

>



--
Ian Collins.
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      05-30-2007
On May 30, 9:33 am, Umesh <(E-Mail Removed)> 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?

 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-30-2007
"Umesh" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> Quentin Godfroy wrote:
>> On May 30, 3:55 am, Umesh <(E-Mail Removed)> 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


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      05-30-2007

"Umesh" <(E-Mail Removed)> 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


 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      05-30-2007
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>
 
Reply With Quote
 
Guru Jois
Guest
Posts: n/a
 
      05-30-2007
On May 30, 1:00 pm, Ian Collins <(E-Mail Removed)> wrote:
> Umesh wrote:
> > Do you have any answer to it? thx.

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


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

Bye
Guru Jois

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
replace a string delimited by 2 other string, regexp problem Sébastien Maurette Ruby 3 10-02-2006 09:40 PM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM
replace instances of text on page with image or other text? juglesh Javascript 16 12-27-2004 04:14 AM



Advertisments