472,982 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

How to delete N lines from the top of a file?

I'm not sure how to do this in php - need to calculate and delete an unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!
Jul 17 '05 #1
4 2600

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an unspecified number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp
Jul 17 '05 #2
the problem with reading the file into an array is that the lines are irregular:

abd245cf | dsgf ; . | d.kjfd | ::kdijh | . | kjdf | | ""

I can't modify what that string looks like, so I'm not sure if I can use
explode.

perhaps I could append the new string on the bottom, then use fseek and fread
to copy what I want into a variable, then just paste it back in - but the syntax
for this is killing me...

developing...
"Garp" <ga***@no7.blueyonder.co.uk> wrote in message
news:hQ*******************@news-text.cableinet.net...

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an

unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp


Jul 17 '05 #3
I couldn't find a way to delete a specific number of "lines" from the top of the
file in an effort to keep the file at 10 lines. But this seems work just as
well by limiting the size of the file - although it's not as precise since I
can't know how long each line will be; $new_visitor is a bunch of data about
visitors that can vary widely.

$fp = fopen($visdata,"a");
fwrite($fp, $new_visitor."\n");
$fp = fopen($visdata,"r");
fseek($fp, -10240, SEEK_END);
$visitors = fread($fp, filesize($visdata));
$fp = fopen($visdata,"w");
fwrite($fp, $visitors);
fclose($fp);
Jul 17 '05 #4
> the problem with reading the file into an array is that the lines are
irregular:

abd245cf | dsgf ; . | d.kjfd | ::kdijh | . | kjdf | | ""

I can't modify what that string looks like, so I'm not sure if I can use
explode.

perhaps I could append the new string on the bottom, then use fseek and fread to copy what I want into a variable, then just paste it back in - but the syntax for this is killing me...

developing...
"Garp" <ga***@no7.blueyonder.co.uk> wrote in message
news:hQ*******************@news-text.cableinet.net...

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an

unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp


I'm going to say three more things, then I'm going to start charging, or
posting my own bugs. 8)

1) Stop top-posting, or I'm-a have to kill you.
2) You asked for a way of cutting all but the bottom ten lines. In my world,
a "line" terminates in a linefeed. file() reads a file in, dividing by
linefeeds and returning an array, and with the minimum of fuss.
3) With the amount of work you've put in to this, you could have learned the
MySQL interfaces.

Still, I wish you luck.

Garp
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Srinivasa T.N. | last post by:
Hi, I want to delete the first 3 lines and last 3 lines of a file. How can I do it? I want to repeat on multiple files.. TIA Regards, Seenu.
2
by: Murali | last post by:
Hello Everyone, I am breaking my head with this problem. Please help me out. Let me first explain my problem : Here it is: I am working in realtime environment where i will be creating...
1
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender,...
4
by: Ignoramus6539 | last post by:
There were some strange requests to my server asking for config.php file (which I do not have in the requested location). I did some investigation. Seems to be a virus written in perl,...
2
by: cdun2 | last post by:
Hello, I have some code that reads each line of a text file, and if a line is found where the length of the string in the line is 384, it writes the line to a text file. The other step that I...
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
3
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
6
by: Annalyzer | last post by:
My database must produces a .csv file with a header line that is different from the detail lines. This .csv file contains a monthly report to an outside agency and is uploaded online so it must...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.