add string into the file (somewhere in the middle)

Hello
I'm wondering howto make such thing:
i would like to add specific string into some configuration files.

but not in the end of file (it's obviously easy) but somewhere in the
middle
for example
in my.cnf after line [mysqld] I'd like to add:
key_buffer = 16M

or in /etc/xinetd.d/echo after lines:
service echo
{
disable = yes

I'd like to add:
wait = yes

of course after my string there has to be rest of file...

any ideas?

regards
jahooo
Jahoo [ Mo, 21 April 2008 21:11 ] [ ID #1947004 ]

Re: add string into the file (somewhere in the middle)

On Mon, 21 Apr 2008 21:11:23 +0200, Jahoo <jahoo [at] o2.pl> wrote:

> Hello
> I'm wondering howto make such thing:
> i would like to add specific string into some configuration files.
>
> but not in the end of file (it's obviously easy) but somewhere in the
> middle
> for example
> in my.cnf after line [mysqld] I'd like to add:
> key_buffer =3D 16M
>
> or in /etc/xinetd.d/echo after lines:
> service echo
> {
> disable =3D yes
>
> I'd like to add:
> wait =3D yes
>
> of course after my string there has to be rest of file...

There's no way to 'insert' it at a specific point, save for loading the =
=

file in memory, making the alteration in the string, and writing it back=
..
-- =

Rik Wasmus
luiheidsgoeroe [ Mo, 21 April 2008 22:03 ] [ ID #1947007 ]

Re: add string into the file (somewhere in the middle)

On Mon, 21 Apr 2008 19:11:23 +0000 (UTC), Jahoo <jahoo [at] o2.pl> wrote in
<fuiosq$vk8$6 [at] news.onet.pl>:

>I'm wondering howto make such thing:
>i would like to add specific string into some configuration files.
>
>but not in the end of file (it's obviously easy) but somewhere in the
>middle
>for example
>in my.cnf after line [mysqld] I'd like to add:
>key_buffer = 16M
>
>or in /etc/xinetd.d/echo after lines:
>service echo
>{
> disable = yes
>
>I'd like to add:
> wait = yes

What you're looking for is an algorithm. It would be something like
this:

open file
get first line
while you have a line
check the line to see if its the one after
which you want to insert lines
if so, insert the lines
get next line
end while
close file

You should start reading the reference on file functions
<http://www.php.net/manual/en/refs.fileprocess.file.php>.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Charles Calvert [ Mo, 21 April 2008 23:12 ] [ ID #1947013 ]

Re: add string into the file (somewhere in the middle)

"Jahoo" <jahoo [at] o2.pl> wrote in message news:fuiosq$vk8$6 [at] news.onet.pl...
> Hello
> I'm wondering howto make such thing:
> i would like to add specific string into some configuration files.
>
> but not in the end of file (it's obviously easy) but somewhere in the
> middle
> for example
> in my.cnf after line [mysqld] I'd like to add:
> key_buffer = 16M
>
> or in /etc/xinetd.d/echo after lines:
> service echo
> {
> disable = yes
>
> I'd like to add:
> wait = yes
>
> of course after my string there has to be rest of file...
>
> any ideas?
>
> regards
> jahooo


Read the file line by line and write each line to a new file. When you get
to the line after which you want to insert extra lines just write those
lines to the new file then continue with the rest. At the end delete the old
file and rename the new file to old file if you have to.
freelance71 [ Di, 22 April 2008 03:23 ] [ ID #1947766 ]

Re: add string into the file (somewhere in the middle)

On Mon, 21 Apr 2008 17:12:21 -0400, Charles Calvert <cbciv [at] yahoo.com>
wrote in <o40q04pn8jh1r7cv2pct4ul66flpn39kug [at] 4ax.com>:

[snip]

Correction:

>What you're looking for is an algorithm. It would be something like
>this:
>
>open file
>get first line
>while you have a line

write the line to the output file

> check the line to see if its the one after
> which you want to insert lines
> if so, insert the lines
> get next line
>end while
>close file
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Charles Calvert [ Di, 22 April 2008 16:03 ] [ ID #1947819 ]
PHP » comp.lang.php » add string into the file (somewhere in the middle)

Vorheriges Thema: Programming skills - growing from amateur to pro
Nächstes Thema: Eclipse.org is using PHP