Read Single Line: fgets()
To read a single line from a file fgets() function is used.
For example: Suppose we have a file 'demo.txt'. The script given below results the first line of the file:
<?php
$filename = fopen("demo.txt", "r") or die("Unable to open file!");
echo fgets($filename);
fclose($filename);
?>
After calling fgets() function, the file pointer moves to the next line.
Read Single Character: fgetc()
To read a single character from a file fgetc() function is used.
For example: Suppose we have a file 'demo.txt'. The script given below reads the file 'demo.txt' character by character until end-of-file:
<?php
$filename = fopen("demo.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($filename)) {
echo fgetc($filename);
}
fclose($filename);
?>
After calling fgetc() function, the file pointer moves to the next character.
0 Comment(s)