Forum Moderators: coopster

Message Too Old, No Replies

How to delete a line from a flat file

         

fintan

3:10 pm on Oct 19, 2004 (gmt 0)

10+ Year Member



Hi I have a flat file database with 5 columns. It's basically an file with a load of email details, with the last columns having three different values. How would I go about searching and deleting the a line from it?

Thanks

fintan

Timotheos

4:51 pm on Oct 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The basics are in this thread
[webmasterworld.com...]

Maybe you could use that to get something going then if you get stuck on some of the details you could come back here with a specific problem.

fintan

10:12 am on Oct 20, 2004 (gmt 0)

10+ Year Member



Here's what I got

$cvsfle1 = "../mail_1.txt";
$cvsfle2 = "../mail_2.txt";

$opnfle1 = fopen($cvsfle1, "r"); // Opens the csv file
$opnfle2 = fopen($cvsfle2,'w');

while ($usrinfo = fscanf($opnfle, "%s %s\t%s\t%s\t%s\n")){
list ($fnme, $lnme, $usrnm, $emladdr, $server) = $usrinfo;

$test = str_replace($_POST["fnme"]." ".$_POST["lnme"]." ".$usrnme." ".$_POST["emladdr"]." ".$_POST["server"]."\r", $rplcmnt, $fnme." ".$lnme." ".$usrnm." ".$emladdr." ".$server."\r");

fwrite($opnfle2,$test);
}

What I need to know is whats the best way to go about over writing the data in $cvsfle1. Would you make sure you have the data written to $cvsfle2 then delete $cvsfle1 and rename $cvsfle2. Or is there a better way? Thanks

fintan.

coopster

10:51 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could simply write it right back out to that file, overwriting anything was there before. Or, if you want to keep a backup copy, rename the old, and write out the new. Really depends on whether or not you want to keep a backup copy of the original I guess.

fintan

9:29 am on Oct 22, 2004 (gmt 0)

10+ Year Member



Backing up the file first would be the smartest thing.
Thanks

fintan

10:26 am on Oct 29, 2004 (gmt 0)

10+ Year Member



Hi I done the delete and the backup. Both work fine but when I search the newly created file I get nothing back. The original file I can do any search and it works fine. Here's the code I'm using.


$msearch = $_POST["msearch"];

$x=0;
print( "\n\n <h3>Matches found:</h3>" );
print( "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n" );

while ($usrinfo = fscanf($opnfle, "%s %s\t%s\t%s\t%s\n")){
list ($fnme, $lnme, $usrnm, $emladdr, $server) = $usrinfo;
$all = $fnme." ".$lnme." ".$usrnm." ".$emladdr." ".$server;
if(preg_match("/\b$msearch\b/i", $all)){
print( "<tr><td>".$fnme." ".$lnme."</td>\n" );
print( "<td>".$usrnm."\n</td>\n" );
print( "<td>".$emladdr."</td>\n" );
print( "<td>".$server."</td><td>");
print( "<form name=\"form".$x++."\" method=\"post\" action=\"delete.php\">" );
print( "<input name=\"fnme\" type=\"hidden\" value=\"".$fnme."\">" );
print( "<input name=\"lnme\" type=\"hidden\" value=\"".$lnme."\">" );
print( "<input name=\"usrnm\" type=\"hidden\" value=\"".$usrnm."\">" );
print( "<input name=\"emladdr\" type=\"hidden\" value=\"".$emladdr."\">" );
print( "<input name=\"server\" type=\"hidden\" value=\"".$server."\">" );
print( "<input type=\"submit\" name=\"mdel\" value=\"Delete\"></form></td></tr>\n" );
}
else{
exit("Sorry no matches found for $msearch");
}
}
print ("</table>");