4 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
5 Description: Parses input from a file according to a format
8 /* Test fscanf() to scan different integer values using different unsigned int format types */
10 $file_path = dirname(__FILE__
);
12 echo "*** Test fscanf(): different unsigned int format types with different integer values ***\n";
15 $filename = "$file_path/fscanf_variation39.tmp";
16 $file_handle = fopen($filename, "w");
17 if($file_handle == false)
18 exit("Error:failed to open file $filename");
20 // different valid integer values
25 -2147483648, // max negative integer value
27 2147483647, // max positive integer value
29 0x123B, // integer as hexadecimal
33 -0x80000000, // max negative integer as hexadecimal
34 0x7fffffff, // max postive integer as hexadecimal
35 0x7FFFFFFF, // max postive integer as hexadecimal
36 0123, // integer as octal
37 01912, // should be quivalent to octal 1
38 -020000000000, // max negative integer as octal
39 017777777777 // max positive integer as octal
41 // various unsigned int formats
42 $unsigned_formats = array( "%u", "%hu", "%lu", "%Lu", " %u", "%u ", "% u", "\t%u", "\n%u", "%4u", "%30u", "%[0-9]", "%*u");
46 // writing to the file
47 foreach($valid_ints as $int_value) {
48 @fprintf
($file_handle, $int_value);
49 @fprintf
($file_handle, "\n");
54 // opening the file for reading
55 $file_handle = fopen($filename, "r");
56 if($file_handle == false) {
57 exit("Error:failed to open file $filename");
61 // reading the values from file using different unsigned int formats
62 foreach($unsigned_formats as $unsigned_format) {
63 // rewind the file so that for every foreach iteration the file pointer starts from bof
65 echo "\n-- iteration $counter --\n";
66 while( !feof($file_handle) ) {
67 var_dump( fscanf($file_handle,$unsigned_format) );
72 echo "\n*** Done ***";
74 $file_path = dirname(__FILE__
);
75 $filename = "$file_path/fscanf_variation39.tmp";