4 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
5 Description: Parses input from a file according to a format
9 Test fscanf() to scan data using different format types and also
10 tracking the file pointer movement along with reading
13 $file_path = dirname(__FILE__
);
15 echo "*** Test fscanf(): tracking file pointer along with reading data from file ***\n";
18 $filename = "$file_path/fscanf_variation55.tmp";
19 $file_handle = fopen($filename, "w");
20 if($file_handle == false)
21 exit("Error:failed to open file $filename");
23 // different valid data
25 12345, // integer value
27 123.45, // float value
29 0x123B, // hexadecimal value
35 10e3
, // exponential value
39 $int_formats = array( "%d", "%f", "%s", "%o", "%x", "%u", "%c", "%e");
43 // writing to the file
44 foreach($valid_data as $data) {
45 @fprintf
($file_handle, $data);
46 @fprintf
($file_handle, "\n");
51 $modes = array("r", "rb", "rt");
53 foreach($modes as $mode) {
55 echo "\n*** File opened in $mode mode ***\n";
56 // opening the file for reading
57 $file_handle = fopen($filename, $mode);
58 if($file_handle == false) {
59 exit("Error:failed to open file $filename");
63 // reading different data from file using different formats
64 foreach($int_formats as $int_format) {
65 // current file pointer position
66 var_dump( ftell($file_handle) );
67 // rewind the file so that for every foreach iteration the file pointer starts from bof
69 // current file pointer position after rewind operation
70 var_dump( ftell($file_handle) );
71 echo "\n-- iteration $counter --\n";
72 while( !feof($file_handle) ) {
73 var_dump( ftell($file_handle) );
74 var_dump( fscanf($file_handle,$int_format) );
77 } // end of inner for loop
78 } // end of outer for loop
80 echo "\n*** Done ***";
82 $file_path = dirname(__FILE__
);
83 $filename = "$file_path/fscanf_variation55.tmp";