|
|
 |
 |
 |
 |
how to replace a string by other string in a text file?
Do you have any answer to it? thx.
Umesh wrote: > Do you have any answer to it? thx.
sed. -- Ian Collins.
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 () { 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 ...")
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.
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?
"Umesh" <fraternitydispo@gmail.com> schrieb im Newsbeitrag news:1180514025.860653.166220@q19g2000prn.googlegroups.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
"Umesh" <fraternitydispo @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
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>
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
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:spamt@clevermonkey.org> Direct replies will be blacklisted. Replace "spamtrap" with my name to contact me directly.
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
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
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 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
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.
|
 |
 |
 |
 |
|