Append text at specific line #?

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.
Pupkin [ Mi, 31 Oktober 2007 19:57 ] [ ID #1858795 ]

Re: Append text at specific line #?

Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
[at] 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.
Good Man [ Mi, 31 Oktober 2007 20:41 ] [ ID #1858796 ]

Re: Append text at specific line #?

"Good Man" <heyho [at] letsgo.com> wrote in message
news:Xns99DA9F95284D5sonicyouth [at] 216.196.97.131...
> Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
> [at] 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

which is about the most un-informed and stupid suggestion from you this
week - i have to quantify/qualify that remark since you are wont to such
inanity.

both the op AND you should RTFM !!!

> 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.

which sounds like a bunch of justification for the load of shit you just
tried to pawn off on the op!
Steve [ Do, 01 November 2007 05:49 ] [ ID #1860129 ]

Re: Append text at specific line #?

"Pupkin" <spamagnet [at] dorrk.com> wrote in message
news:MPG.21926fd8833b9cbb9897e5 [at] 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.

fseek() ... either php.net or google.
Steve [ Do, 01 November 2007 05:50 ] [ ID #1860130 ]

Re: Append text at specific line #?

..oO(Steve)

>"Good Man" <heyho [at] letsgo.com> wrote in message
>news:Xns99DA9F95284D5sonicyouth [at] 216.196.97.131...
>> Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
>> [at] news.giganews.com:
>>
>>> 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?
>>
>> 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
>
>which is about the most un-informed and stupid suggestion from you this
>week - i have to quantify/qualify that remark since you are wont to such
>inanity.
>
>both the op AND you should RTFM !!!

Ever tried to use fseek() to insert data into a file?

Micha
Michael Fesser [ Do, 01 November 2007 06:24 ] [ ID #1860131 ]

Re: Append text at specific line #?

On Wed, 31 Oct 2007 23:49:15 -0500, in alt.php "Steve"
<no.one [at] example.com>
<NVcWi.501$_P3.446 [at] newsfe05.lga> wrote:

>|
>| "Good Man" <heyho [at] letsgo.com> wrote in message
>| news:Xns99DA9F95284D5sonicyouth [at] 216.196.97.131...
>| > Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
>| > [at] 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
>|
>| which is about the most un-informed and stupid suggestion from you this
>| week - i have to quantify/qualify that remark since you are wont to such
>| inanity.
>|
>| both the op AND you should RTFM !!!

I suggest you take your own advice.
If the file was fixed width format (all lines being the same length)
then you would:
read the entire file into memory
calculate the offset position
make the changes to the required 'line'
save the entire file back

If the lines are of differing lengths then how would you know what
offset position to use with an fseek?

The only method is what Good Man suggested.

>| > 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.
>|
>| which sounds like a bunch of justification for the load of shit you just
>| tried to pawn off on the op!
>|
-- ------------------------------------------------------------ -
jnorthau [at] yourpantsyahoo.com.au : Remove your pants to reply
-- ------------------------------------------------------------ -
Jeff North [ Do, 01 November 2007 07:55 ] [ ID #1860132 ]

Re: Append text at specific line #?

Good Man wrote:
> Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
> [at] 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 [at] attglobal.net
==================
Jerry Stuckle [ Do, 01 November 2007 12:23 ] [ ID #1860133 ]

Re: Append text at specific line #?

Steve wrote:
> "Good Man" <heyho [at] letsgo.com> wrote in message
> news:Xns99DA9F95284D5sonicyouth [at] 216.196.97.131...
>> Pupkin <spamagnet [at] dorrk.com> wrote in news:MPG.21926fd8833b9cbb9897e5
>> [at] 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
>
> which is about the most un-informed and stupid suggestion from you this
> week - i have to quantify/qualify that remark since you are wont to such
> inanity.
>
> both the op AND you should RTFM !!!
>
>> 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.
>
> which sounds like a bunch of justification for the load of shit you just
> tried to pawn off on the op!
>
>
>

You should learn to understand how programs work. Good Man's
recommendation is correct.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Do, 01 November 2007 12:24 ] [ ID #1860134 ]

Re: Append text at specific line #?

Steve wrote:
> "Pupkin" <spamagnet [at] dorrk.com> wrote in message
> news:MPG.21926fd8833b9cbb9897e5 [at] 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.
>
> fseek() ... either php.net or google.
>
>
>

And now how are you going to INSERT text at that point?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Do, 01 November 2007 12:24 ] [ ID #1860135 ]
PHP » alt.php » Append text at specific line #?

Vorheriges Thema: PHP & AJAX Form Validation
Nächstes Thema: Need php5 soap example dealing with complex types