Page 1 of 1

Issue in fwrite/fputs.

Posted: Sun Aug 21, 2011 5:49 pm
by veena
Hi all,

I am new to php and trying to add/change/delete records from notepad. Would you guys pls help me. thanks in advance
=======================================================
My Sample data is in events.txt:

001, 20110720, Baby shower
002, 20110605, Health test
123, 20000000, test record
003, 20110701, Doctor visit
004, 20110905, Picknic

I am calling function find_Record with a parm $newid1. Assume that its value is 123

For now I commented out the fwrite line, it is echoing ($buffer1) the 4 records except
"123, 20000000, test record".

However if I remove the comment from fwrite then it is echoing $buffer1 as
001, 20110720, Baby shower
27
0003, 20110701, Doctor visit
28

and now the data in events.txt is:
001, 20110720, Baby shower
001, 20110720, Baby shower
123, 20000000, test record
003, 20110701, Doctor visit
003, 20110701, Doctor visit
=======================================================

Code: Select all

function find_Record($newid1) { 

$handle1 = @fopen("C:\events.txt", "r+") or die("Oops, could not open file"); 

    while (!feof($handle1)) { 
                 
        $buffer1 = stream_get_line($handle1, 4096, "\n"); 
                $id1 = substr(htmlentities($buffer1), 0, 3); 
         
        if($id1 != $newid1){ 
             
        echo $buffer1; 
        echo nl2br("\n"); 
        // echo fwrite($handle1, $buffer1);  
             
        }   } 
     
    fclose($handle1); 
    return true;     
}  
=======================================================
I want to delete the record starts with 123 and rewrite event.txt with remaining 4 records.
I tried with fputs too. I tried using dot operator & "\r\n" for next line in fwrite/fputs. I am not sure what I am doing wrong.
=======================================================

Re: Issue in fwrite/fputs.

Posted: Sun Aug 21, 2011 7:06 pm
by Christopher
You might want to look at the CSV functions in PHP, since you have CSV data. They will allow you to read your file, get the data in arrays and write it out again.

Re: Issue in fwrite/fputs.

Posted: Sun Aug 21, 2011 9:51 pm
by veena
Hi Christopher,

Thanks for your reply.
Actually I found the solution on http://www.webmasterworld.com/php/3304270.htm.

Regards,
Veena