TXT file operation problem, urgent

Source: Internet
Author: User
Write a string into a document with fwrite How do you get the pointer to stop at the end of the last line instead of the next line?
Each time after the string is added Php_eol or "\ r \ n" To finish writing the last line, the document looks like this
"String 1"
"String 2"
But actually I use Fgets (code below) when reading
$fname 1= ("Pending.txt"), $fp 1=fopen ($fname 1, "r+"), while (!feof ($fp 1)) {$read =explode (";", Fgets ($fp 1));//String To hit the group echo "read[0]=". $read [0];//used to test echo.]
";} Fclose ($fp 1);

The test results are as follows
read[0]= VVV@222.DPN
read[0]= VVV@333.DPN
read[0]=
Why do we have one more because I write the last line of string. A blank space, in the TXT document can be read by the line read.
How can we avoid this problem? Because of the need I have to use fgets (), if the function allows me to use file_get_contents () to say it.
Ask the big God for solutions


Reply to discussion (solution)

What's wrong with it?
Your while (!feof ($fp 1)) is only set when the end of the file is read
And the last line of Fgets ($fp 1) just finished reading the content, and did not read the end of the file
To the next round of wiping will read the end of the file

So I can write.

while ($buf = fgets ($fp 1)) {       $read =explode (";", $buf);//String, to hit the group    echo "read[0]=". $read [0];//used to test    Echo "
";}

What's wrong with it?
Your while (!feof ($fp 1)) is only set when the end of the file is read
And the last line of Fgets ($fp 1) just finished reading the content, and did not read the end of the file
To the next round of wiping will read the end of the file

So I can write.

while ($buf = fgets ($fp 1)) {       $read =explode (";", $buf);//String, to hit the group    echo "read[0]=". $read [0];//used to test    Echo "
";}


Thanks, this problem is caused by the number of rows I have quoted and deleted. I want to delete a row, the original code is this
while (!feof ($fp 1)) {       $read =explode (";", Fgets ($fp 1)),//Get the line in TXT and hit the array    if (read[0]==userinfo_en[0]& &READ[2]==USERINFO_EN[2])    {       file_put_contents ($file, fgets ($fp 1));}    } Fclose ($fp 1);//userinfor_en = username  ; password; CCN; exp; cvv; type;//                                   0         1                   2          3        4         5// This is obtained from other places with get, meaning that the execution of the time if//read to receive the data will be included in the receipt of the entire deletion, but I used the direct addition to the line, the remaining lines are all deleted. This is the crux of the question, what should I do//solve? I've been thinking about this for a long time
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.