473,320 Members | 1,870 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,320 software developers and data experts.

how to delete last line of file

I have a flat file on the server which I append to from a HTML web form.
I need to delete the last line of the flat file before I append though.

How do i do that?

I have the following code. I'm not sure if it's correct.

$pattern = "</member>"; // what I want to look for.
$ora_books = preg_grep($pattern, file('/path/to/your/file.txt'));
Kenneth
Jul 17 '05 #1
11 12402
file() returns an array.

See: http://us2.php.net/file_get_contents/

-Mike

--
Melt away the Cellulite with Cellulean!
http://www.MeltAwayCellulite.com/
"Kenneth" <ja********@hotmail.com> wrote in message
news:d4**********@newsmaster.cc.columbia.edu...
I have a flat file on the server which I append to from a HTML web form.
I need to delete the last line of the flat file before I append though.

How do i do that?

I have the following code. I'm not sure if it's correct.

$pattern = "</member>"; // what I want to look for.
$ora_books = preg_grep($pattern, file('/path/to/your/file.txt'));
Kenneth

Jul 17 '05 #2
Kenneth wrote:
I have a flat file on the server which I append to from a HTML web form. I need to delete the last line of the flat file before I append

though.
<snip>

http://in.php.net/fseek#37147

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #3
R. Rajesh Jeba Anbiah wrote:
Kenneth wrote:
I have a flat file on the server which I append to from a HTML web


form.
I need to delete the last line of the flat file before I append


though.
<snip>

http://in.php.net/fseek#37147

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

I figured out how to get the last line of the file but I do not know how
to delete the line without creating a temp file.

I am assuming it's a long complicated function.
Jul 17 '05 #4

Kenneth wrote:
I figured out how to get the last line of the file but I do not know how to delete the line without creating a temp file.

I am assuming it's a long complicated function.


<?
$inp = file('yourfile.name');
$out = fopen('yourfile.name','w');
for ($I=0;$i<count($inp)-1);$i++)
fwrite($out,$inp[$I]);
fclose($out)l
?>

Not long and complicated at all...

1) Read file into an array
2) Open the new file for write
3) Write all except last line to the new file
4) Close the new file

Ken

Jul 17 '05 #5
Kenneth wrote:
[snip]
I figured out how to get the last line of the file but I do not know how
to delete the line without creating a temp file.

I am assuming it's a long complicated function.


No not really.

If you have found the last line, then you are probably pretty close to
just do:

ftruncate($fp, ftell($fp));
/Bent
Jul 17 '05 #6
THis is what i have so far.

---------------------------------
$line_counter = 0;
$desired_line = 29;

$fh = fopen('employees.txt','r+') or die($php_errormsg);
while ((! feof($fh)) && ($line_counter <= $desired_line)) {
if ($s = fgets($fh,1048576)) {
$line_counter++;
}
}
fclose($fh) or die($php_errormsg);

print $s;
----------------------------------

Bent Stigsen wrote:
Kenneth wrote:
[snip]
I figured out how to get the last line of the file but I do not know
how to delete the line without creating a temp file.

I am assuming it's a long complicated function.

No not really.

If you have found the last line, then you are probably pretty close to
just do:

ftruncate($fp, ftell($fp));
/Bent


Jul 17 '05 #7
Here is what I have but it deletes everything except the first line and
part of the second line.

-------------------------------------------------------------------
$line_counter = 0;
$desired_line = 29;

$fh = fopen('employees.txt','a+') or die($php_errormsg);

while ((! feof($fh)) && ($line_counter <= $desired_line)) {
if ($s = fgets($fh,1048576)) {
$line_counter++;
}
}
rewind($fh);
if (-1 == fwrite($fh,$s)){ die($php_errormsg); }

// adjust the file's length to just what's been written
ftruncate($fh,ftell($fh)) or die($php_errormsg);

// close the file
fclose($fh) or die($php_errormsg);

print $s;

---------------------------------------------------------------------
Kenneth wrote:
I have a flat file on the server which I append to from a HTML web form.
I need to delete the last line of the flat file before I append though.

How do i do that?

I have the following code. I'm not sure if it's correct.

$pattern = "</member>"; // what I want to look for.
$ora_books = preg_grep($pattern, file('/path/to/your/file.txt'));
Kenneth

Jul 17 '05 #8
Ken Robinson wrote:
Kenneth wrote: Ken, I tried your code. Everything fits logically but i keep getting
following error message;
Parse error: syntax error, unexpected ')', expecting ';' in
c:\Inetpub\wwwroot\final\test\lastline.php on line 13
But I can't find where there is a syntax error.


I figured out how to get the last line of the file but I do not know


how
to delete the line without creating a temp file.

I am assuming it's a long complicated function.

<?
$inp = file('yourfile.name');
$out = fopen('yourfile.name','w');
for ($I=0;$i<count($inp)-1);$i++)
fwrite($out,$inp[$I]);
fclose($out)l
?>

Not long and complicated at all...

1) Read file into an array
2) Open the new file for write
3) Write all except last line to the new file
4) Close the new file

Ken

Jul 17 '05 #9

Kenneth wrote:
Ken Robinson wrote:
Kenneth wrote: Ken, I tried your code. Everything fits logically but i keep getting

following error message;
Parse error: syntax error, unexpected ')', expecting ';' in
c:\Inetpub\wwwroot\final\test\lastline.php on line 13


I (obviously) had a few typo's in the code I posted. It should read:
<?
$inp = file('yourfile.name');
$out = fopen('yourfile.name','w');
for ($i=0;$i<count($inp)-1;$i++)
fwrite($out,$inp[$i]);
fclose($out);
?>

You should always take any code posted with a grain of salt and don't
just copy it verbatim. Always check for errors in both the syntax and
logic.

Ken

Jul 17 '05 #10
Kenneth wrote:
[snip]

Without knowing the size of your file, or how it otherwise is accessed,
the generic advice would be to avoid using file() or looping through the
whole file with fgets(). And to work on a copy instead of a "live" version.

Take a look (again) at the link, which R. Rajesh Jeba Anbiah gave you.
The idea is thant you search backwards and look for some marker (which
may or may not be a newline, as it is not required for the xml-document
to be valid). Once positioned at the right spot in the file, you can
start overwriting with new data, perhaps truncating the file first.
A quick and dirty way to your specific problem, could be something like...

$end_string = "</member>\n";
$length_end_string = strlen($end_string);

$fh = fopen('employees.txt','r+');
fseek($fh, -$length_end_string, SEEK_END);

fwrite($fh, "some string that needs to be added");

fwrite($fh, $end_string);
fclose($fh);

It is of course required that any script modifying the file, all write
the same string in the end.
/Bent
Jul 17 '05 #11
Ken Robinson wrote:
Kenneth wrote:

No Ken, you were missing and end bracket.

Anyway here is what I have so far. It is working now but I think it
takes too much resources. Let me know if you can improve the following
code somehow.

-----------------------------------------------------------------------------------------

//Search for the file
$key = "</members>";

//load file into $fc array
$fc=file("members.xml");

//open same file and use "w" to clear file
$f=fopen("members.xml","w");

//loop through array using foreach
foreach($fc as $line)
{
if (!strstr($line,$key)) //look for $key in each line
fputs($f,$line); //place $line back in file
}
fclose($f);

-------------------------------------------------------------------------------------------------
The problem is that I have to file() which I heard takes up to much
resources, but it works. Can I replace the code somehow?


Ken Robinson wrote:
Kenneth wrote:


Ken, I tried your code. Everything fits logically but i keep getting


following error message;
Parse error: syntax error, unexpected ')', expecting ';' in
c:\Inetpub\wwwroot\final\test\lastline.php on line 13

I (obviously) had a few typo's in the code I posted. It should read:
<?
$inp = file('yourfile.name');
$out = fopen('yourfile.name','w');
for ($i=0;$i<count($inp)-1;$i++)
fwrite($out,$inp[$i]);
fclose($out);
?>

You should always take any code posted with a grain of salt and don't
just copy it verbatim. Always check for errors in both the syntax and
logic.

Ken

Jul 17 '05 #12

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

Similar topics

4
by: Tor Erik Sønvisen | last post by:
Hi How can I read the first line of a file and then delete this line, so that line 2 is line 1 on next read? regards
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.
6
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
3
by: Cc | last post by:
how do i delete last append line?
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,...
10
by: Joah Senegal | last post by:
Hello all, I want something that should be realy really simple.... but I dont get it.. searched google and stuf, but found nothing useful. I;m opening a txt-file with a fstream. Now I want to...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.