A TXT file operation problem occurs. how can I stop the pointer at the end of the last row instead of the next row in the document by using fwrite?
Every time you add PHP_EOL or "\ r \ n" to the end of the string, you can see this in the document.
"String 1"
"String 2"
But when I use fgets (the code is as follows) to read
$ Fname1 = ("pending.txt"); $ fp1 = fopen ($ fname1, "r +"); while (! Feof ($ fp1) {$ read = explode (";", fgets ($ fp1); // used in strings; to compress into an array echo "read [0] = ". $ read [0]; // used to test echo"
";} Fclose ($ fp1 );
The test results are as follows:
Read [0] = vvv@222.dpn
Read [0] = vvv@333.dpn
Read [0] =
Why is one more? it is because when the last string is written. After a space, you can read the TXT file by row.
How can we avoid this problem? I have to use fgets () because of the requirement. if the function permits me to use file_get_contents (), that's all ..
Please give the solution
Reply to discussion (solution)
Is there anything wrong?
Your while (! Feof ($ fp1) is valid only when the end of the file is read.
The fgets ($ fp1) in the last row only finishes reading the content and does not read the end of the file.
In the next round, the system will read the end of the file.
So you can write
While ($ buf = fgets ($ fp1) {$ read = explode (";", $ buf); // used in strings; echo "read [0] = ". $ read [0]; // used to test echo"
";}
Is there anything wrong?
Your while (! Feof ($ fp1) is valid only when the end of the file is read.
The fgets ($ fp1) in the last row only finishes reading the content and does not read the end of the file.
In the next round, the system will read the end of the file.
So you can write
While ($ buf = fgets ($ fp1) {$ read = explode (";", $ buf); // used in strings; echo "read [0] = ". $ read [0]; // used to test echo"
";}
Thanks. this problem is caused by the number of rows in the reference and deletion. I want to delete a line. The original code is
While (! Feof ($ fp1) {$ read = explode (";", fgets ($ fp1 )); // Obtain the rows in the txt file and compress them into an array if (read [0] = userinfo_en [0] & read [2] = userinfo_en [2]) {file_put_contents ($ file, fgets ($ fp1) ;}} fclose ($ fp1); // userinfor_en = username; password; ccn; exp; cvv; type; // 0 1 2 3 4 5 // This is obtained from somewhere else with GET, this means that when executing the command, if // reads the received data, it will delete all the rows that contain the received data. However, after I use the command, all the other rows will be deleted. This is the core issue. how can I solve it? I have been thinking for a long time.