About PHP using Fread to read a file will always be more than one empty character of the problem

Source: Internet
Author: User
Tags fread
Questions about PHP using Fread to read files will always be one more empty character
The code is as follows:

PHP Code
  
   $fileSize = FileSize ($filePath), $handle = fopen ($filePath, "RB"), while (!feof ($handle)) {        var_dump (fread ($handle), $fileSize));        Will output one more empty character}



For advice, thank you!

------Solution--------------------
Fread ($handle, $fileSize)
The file has exactly $fileSize bytes of content, read smoothly, so the program did not detect the end of the file. Then the second loop is executed, and the pointer is at the last, and there is nothing to read, so the system only sets the file pointer to the bottom, so that feof () returns True. PHP operation file System and C almost no difference, this aspect has about C feof () has a very detailed explanation
------Solution--------------------
In fact, I did not say a point. As for the solution, it can be $fileSize +1. Or you can do it completely without cyclic fread ($handle, FileSize ($filename)); To read all the file contents.
------Solution--------------------
Trim () a bit?
------Solution--------------------
If the file is not large, read it all to the array and remove the last one. If the file is large, another thought of its method, but did not find much of this will affect what ah. Really affect, want to get rid of should also not be difficult.
------Solution--------------------
Because you are the Windows platform, the file is the text to open the stored content, at the end there will be a special byte to identify the end of the file, you can use the RB open nature will be able to read the last special byte.

Open with R, Fgets read it.
  • 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.