Reply to Re: Append text at specific line #?

Your name:

Reply:


Posted by Jerry Stuckle on 11/01/07 11:23

Good Man wrote:
> Pupkin <spamagnet@dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
> @news.giganews.com:
>
>> Hi,
>>
>> If I'm using fwrite to append text to a file, is it possible to set the
>> pointer to a specific line # at which to insert the text, rather than
>> the beginning or end of the file?
>>
>> Thanks.
>>
>
> the easiest way would be something along the lines of:
>
> 1) fread your source file into a variable
> 2) fopen a new file for writing
> 3) go through your source file line-by-line using a loop
> 4) for each line you read, fwrite it to your new file (keeping an eye out
> for the line number you want to change... when you find it, change it and
> write it to your new file)
> 5) close the files
>
> your source file remains unchanged (delete it if you want) and you have a
> new file that you can use.... and of course you can just copy/rename this
> new file to have the same one as your old one, so that it's next up for
> reading/editing.
>
>
>
>

If the file isn't too big, I would rather use file() to load the
contents into an array. It's then a simple matter to find the
appropriate line, insert the text and write the entire file out again.

OTOH, if the file is large, this is definitely the way to go.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация