473,437 Members | 1,773 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,437 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 12411
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
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.