473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2630

"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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.