473,221 Members | 1,864 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,221 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 12392
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 online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.