import zend standard tests
[hiphop-php.git] / hphp / test / zend / bad / ext-standard-file / fscanf_variation55.php
blobf59a8dc789c184c5d491d1e1d6afe53583d2109f
1 <?php
3 /*
4 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
5 Description: Parses input from a file according to a format
6 */
8 /*
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";
17 // create a file
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
24 $valid_data = array(
25 12345, // integer value
26 -12345,
27 123.45, // float value
28 -123.45,
29 0x123B, // hexadecimal value
30 0x12ab,
31 0123, // octal value
32 -0123,
33 "abcde", // string
34 'abcde',
35 10e3, // exponential value
36 10e-3
38 // various formats
39 $int_formats = array( "%d", "%f", "%s", "%o", "%x", "%u", "%c", "%e");
41 $counter = 1;
43 // writing to the file
44 foreach($valid_data as $data) {
45 @fprintf($file_handle, $data);
46 @fprintf($file_handle, "\n");
48 // closing the file
49 fclose($file_handle);
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");
62 $counter = 1;
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
68 rewind($file_handle);
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) );
76 $counter++;
77 } // end of inner for loop
78 } // end of outer for loop
80 echo "\n*** Done ***";
81 ?><?php
82 $file_path = dirname(__FILE__);
83 $filename = "$file_path/fscanf_variation55.tmp";
84 unlink($filename);