Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to replace a string by other string in a text file? (http://www.velocityreviews.com/forums/t510529-how-to-replace-a-string-by-other-string-in-a-text-file.html)

Umesh 05-30-2007 07:55 AM

how to replace a string by other string in a text file?
 
Do you have any answer to it? thx.


Ian Collins 05-30-2007 08:00 AM

Re: how to replace a string by other string in a text file?
 
Umesh wrote:
> Do you have any answer to it? thx.
>

sed.

--
Ian Collins.

Quentin Godfroy 05-30-2007 08:04 AM

Re: how to replace a string by other string in a text file?
 
On May 30, 3:55 am, Umesh <fraternitydispo...@gmail.com> wrote:
> Do you have any answer to it? thx.



system("sed ...")


Umesh 05-30-2007 08:33 AM

Re: how to replace a string by other string in a text file?
 
#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.com> wrote:
> > Do you have any answer to it? thx.

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



Ian Collins 05-30-2007 08:39 AM

Re: how to replace a string by other string in a text file?
 
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.com> wrote:
>>> Do you have any answer to it? thx.

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

>



--
Ian Collins.

mark_bluemel@pobox.com 05-30-2007 09:48 AM

Re: how to replace a string by other string in a text file?
 
On May 30, 9:33 am, Umesh <fraternitydispo...@gmail.com> 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?


Joachim Schmitz 05-30-2007 09:54 AM

Re: how to replace a string by other string in a text file?
 
"Umesh" <fraternitydisposal@gmail.com> schrieb im Newsbeitrag
news:1180514025.860653.166220@q19g2000prn.googlegr oups.com...
> Quentin Godfroy wrote:
>> On May 30, 3:55 am, Umesh <fraternitydispo...@gmail.com> 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



Robbie Hatley 05-30-2007 10:08 AM

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

"Umesh" <fraternitydisposal@gmail.com> 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



Tor Rustad 05-30-2007 11:28 AM

Re: how to replace a string by other string in a text file?
 
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>

Guru Jois 05-30-2007 01:53 PM

Re: how to replace a string by other string in a text file?
 
On May 30, 1:00 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Umesh wrote:
> > Do you have any answer to it? thx.

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


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

Bye
Guru Jois



All times are GMT. The time now is 03:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.