Textfile Delete Row
Moderator: General Moderators
-
tylerrowsell
- Forum Newbie
- Posts: 5
- Joined: Wed May 27, 2009 6:42 pm
Textfile Delete Row
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
Thanks
Re: Textfile Delete Row
You are using any database.. like MySQL ?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
-
tylerrowsell
- Forum Newbie
- Posts: 5
- Joined: Wed May 27, 2009 6:42 pm
Re: Textfile Delete Row
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
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.
-
tylerrowsell
- Forum Newbie
- Posts: 5
- Joined: Wed May 27, 2009 6:42 pm
Re: Textfile Delete Row
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
Thanks though
it should not be to hard to do that because it is only the first row that needs to be deleted
-
tylerrowsell
- Forum Newbie
- Posts: 5
- Joined: Wed May 27, 2009 6:42 pm
Re: Textfile Delete Row
That is excellent much better an easier then what i had put in... thanks a lot !!
Re: Textfile Delete Row
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
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 
Edit:
This one works correctly, still I feel some refactoring needed:
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);
?>-
tylerrowsell
- Forum Newbie
- Posts: 5
- Joined: Wed May 27, 2009 6:42 pm
Re: Textfile Delete Row
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
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
Only $lineToDelete should be changed.