- $fp = fopen ("Test.txt", "R");
- ?>
Copy Code2.fclose (Close file) syntax: fclose (filepointer) filepointer, the file pointer to be closed. If successful, the Fclose function returns TRUE if the Fclose function returns FALSE if it fails. Example:
- $fp = fopen ("Test.txt", "R");
- Fclose ($FP);
- ?>
Copy Code3.feof (detects whether the end of file has been reached) syntax: feof (filepointer) Filepointer, the pointer to the file to be detected, must point to a file that was successfully opened without closing. The feof function returns TRUE if the file pointer is to the end of the file or if an error occurs. Example:
- $fp = fopen ("Test.txt", "R");
- while (! feof ($FP))
- {
- Echo fgets ($FP). "
";
- }
- Fclose ($FP);
- ?>
Copy Code4.fgets (reads a line from the file pointer) syntax: fgets (filepointer) filepointer, the file pointer to be read. If successful, reads a row from the file and returns a string, FALSE if it fails. Example:
- $fp = fopen ("Test.txt", "R");
- if ($FP)
- {
- For ($i =1;! feof ($fp); $i + +)
- {
- echo "line". $i. ":". Fgets ($FP). "
";
- }
- }
- Else
- {
- echo "Failed to open file";
- }
- Fclose ($FP);
- ?>
Copy CodeSuppose Test.txt's content is: Hello Worldhello cnblogshello Heihaozihello The Everyone page output results in: Row 1:hello World row 2:hello cnblogs line 3:hello H Eihaozi Line 4:hello Everyone 5.fwrite (write file) syntax: fwrite (filepointer,string) filepointer, the file pointer to be written. String that is to be written. If successful, returns the number of characters written, or FALSE if it fails. Example:
- $fp = fopen ("Test.txt", "w");//file is emptied and then written
- if ($FP)
- {
- $count = 0;
- for ($i =1; $i <=5; $i + +)
- {
- $flag =fwrite ($fp, "line". $i. ":". " Hello world!\r\n ");
- if (! $flag)
- {
- echo "Failed to write to file
";
- Break
- }
- $count + = $flag;
- }
- echo "Total write". $count. " Characters ";
- }
- Else
- {
- echo "Failed to open file";
- }
- Fclose ($FP);
- ?>
Copy CodeThe result of the page output is: Write 100 characters in total The Test.txt file is written: line 1:hello world! row 2:hello world! row 3:hello world! row 4:hello world! row 5:hello world! |