Issue in fwrite/fputs.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
veena
Forum Newbie
Posts: 2
Joined: Sun Aug 21, 2011 5:40 pm

Issue in fwrite/fputs.

Post 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.
=======================================================
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Issue in fwrite/fputs.

Post 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.
(#10850)
veena
Forum Newbie
Posts: 2
Joined: Sun Aug 21, 2011 5:40 pm

Re: Issue in fwrite/fputs.

Post by veena »

Hi Christopher,

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

Regards,
Veena
Post Reply