Page 1 of 1

Textfile Delete Row

Posted: Wed May 27, 2009 6:45 pm
by tylerrowsell
Im making like a guest book and each new entry i saved on a new row in a text file is there a way i can choose a specific row and delete it?

Thanks

Re: Textfile Delete Row

Posted: Thu May 28, 2009 2:46 am
by anand
tylerrowsell wrote:Im making like a guest book and each new entry i saved on a new row in a text file is there a way i can choose a specific row and delete it?

Thanks
You are using any database.. like MySQL ?

Re: Textfile Delete Row

Posted: Thu May 28, 2009 4:50 am
by tylerrowsell
nope im just using a txt file no mysql or anything... like i know how to select a specfic row but i don't know how to delete it

Re: Textfile Delete Row

Posted: Thu May 28, 2009 4:57 am
by onion2k
You'll need to open the file, put everything you want up to the row you want to delete into an array, skip the row to be deleted, then read the rest into the array, and finally write the whole lot back to the text file. It'll be slow and horrible. Use a database.

Re: Textfile Delete Row

Posted: Thu May 28, 2009 5:24 am
by tylerrowsell
i would use a asp because i know that language better but our server does not support that and it does not support databases

Thanks though

it should not be to hard to do that because it is only the first row that needs to be deleted

Re: Textfile Delete Row

Posted: Thu May 28, 2009 5:08 pm
by tylerrowsell
That is excellent much better an easier then what i had put in... thanks a lot !!

Re: Textfile Delete Row

Posted: Thu May 28, 2009 5:32 pm
by Darhazer
No need to read the whole file... Since this is my 100th post, here is the proper solution (I didn't find anyone written this on Google). Not perfect, there is a bug list the last line, but's here 1:30 AM

Code: Select all

 
<?php
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
$write = true;
 
$position = ftell($file);
while ($line = fgets($file)) {
   $newLine = fgets($file);
   if ($newLine == false) break;
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
}
fclose($file);
echo 'Done';
?>
 

Re: Textfile Delete Row

Posted: Fri May 29, 2009 12:58 pm
by Darhazer
Actually in the code above, in the second while fread() with fixed size of buffer should be used instead of fgets(). Later this week I will edit the post with the correct code, if anyone don't fix this before me :mrgreen:

Edit:
This one works correctly, still I feel some refactoring needed:

Code: Select all

<?php
$size = filesize('test.txt');
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
 
$position = ftell($file);
$lineToRemove = fgets($file);
$bufferSize = strlen($lineToRemove);
while ($newLine = fread($file, $bufferSize)) {
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
   fseek($file, $bufferSize, SEEK_CUR);
   
}
ftruncate($file, $size - $bufferSize);
echo 'Done';
fclose($file);
?>

Re: Textfile Delete Row

Posted: Fri Jun 19, 2009 6:29 am
by tylerrowsell
to do this what would need to be changed just the lineToDelete so if that changes to 4 it will delete line 4 do i need to change counter?
Darhazer wrote:Actually in the code above, in the second while fread() with fixed size of buffer should be used instead of fgets(). Later this week I will edit the post with the correct code, if anyone don't fix this before me :mrgreen:

Edit:
This one works correctly, still I feel some refactoring needed:

Code: Select all

<?php
$size = filesize('test.txt');
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
 
$position = ftell($file);
$lineToRemove = fgets($file);
$bufferSize = strlen($lineToRemove);
while ($newLine = fread($file, $bufferSize)) {
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
   fseek($file, $bufferSize, SEEK_CUR);
   
}
ftruncate($file, $size - $bufferSize);
echo 'Done';
fclose($file);
?>

Re: Textfile Delete Row

Posted: Fri Jun 19, 2009 4:22 pm
by Darhazer
Only $lineToDelete should be changed.