1--TEST--
2Test fscanf() function: usage variations - float formats with resource
3--FILE--
4<?php
5
6/*
7  Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
8  Description: Parses input from a file according to a format
9*/
10
11/* Test fscanf() to scan resource type using different float format types */
12
13$file_path = __DIR__;
14
15echo "*** Test fscanf(): different float format types with resource ***\n";
16
17// create a file
18$filename = "$file_path/fscanf_variation10.tmp";
19$file_handle = fopen($filename, "w");
20if($file_handle == false)
21  exit("Error:failed to open file $filename");
22
23
24// resource type variable
25$fp = fopen (__FILE__, "r");
26$dfp = opendir ( __DIR__ );
27
28// array of resource types
29$resource_types = array (
30  $fp,
31  $dfp
32);
33
34$float_formats = array( "%f",
35                      "%hf", "%lf", "%Lf",
36                      " %f", "%f ", "% f",
37                      "\t%f", "\n%f", "%4f",
38                      "%30f", "%[0-9]", "%*f"
39               );
40
41$counter = 1;
42
43// writing to the file
44foreach($resource_types as $value) {
45  @fprintf($file_handle, $value);
46  @fprintf($file_handle, "\n");
47}
48// closing the file
49fclose($file_handle);
50
51// opening the file for reading
52$file_handle = fopen($filename, "r");
53if($file_handle == false) {
54  exit("Error:failed to open file $filename");
55}
56
57$counter = 1;
58// reading the values from file using different formats formats
59foreach($float_formats as $float_format) {
60  // rewind the file so that for every foreach iteration the file pointer starts from bof
61  rewind($file_handle);
62  echo "\n-- iteration $counter --\n";
63  while( !feof($file_handle) ) {
64    var_dump( fscanf($file_handle,$float_format) );
65  }
66  $counter++;
67}
68
69// closing the resources
70fclose($fp);
71closedir($dfp);
72
73echo "\n*** Done ***";
74?>
75--CLEAN--
76<?php
77$file_path = __DIR__;
78$filename = "$file_path/fscanf_variation10.tmp";
79unlink($filename);
80?>
81--EXPECTF--
82*** Test fscanf(): different float format types with resource ***
83
84-- iteration 1 --
85array(1) {
86  [0]=>
87  NULL
88}
89array(1) {
90  [0]=>
91  NULL
92}
93bool(false)
94
95-- iteration 2 --
96array(1) {
97  [0]=>
98  NULL
99}
100array(1) {
101  [0]=>
102  NULL
103}
104bool(false)
105
106-- iteration 3 --
107array(1) {
108  [0]=>
109  NULL
110}
111array(1) {
112  [0]=>
113  NULL
114}
115bool(false)
116
117-- iteration 4 --
118array(1) {
119  [0]=>
120  NULL
121}
122array(1) {
123  [0]=>
124  NULL
125}
126bool(false)
127
128-- iteration 5 --
129array(1) {
130  [0]=>
131  NULL
132}
133array(1) {
134  [0]=>
135  NULL
136}
137bool(false)
138
139-- iteration 6 --
140array(1) {
141  [0]=>
142  NULL
143}
144array(1) {
145  [0]=>
146  NULL
147}
148bool(false)
149
150-- iteration 7 --
151
152Warning: fscanf(): Bad scan conversion character " " in %s on line %d
153NULL
154
155Warning: fscanf(): Bad scan conversion character " " in %s on line %d
156NULL
157bool(false)
158
159-- iteration 8 --
160array(1) {
161  [0]=>
162  NULL
163}
164array(1) {
165  [0]=>
166  NULL
167}
168bool(false)
169
170-- iteration 9 --
171array(1) {
172  [0]=>
173  NULL
174}
175array(1) {
176  [0]=>
177  NULL
178}
179bool(false)
180
181-- iteration 10 --
182array(1) {
183  [0]=>
184  NULL
185}
186array(1) {
187  [0]=>
188  NULL
189}
190bool(false)
191
192-- iteration 11 --
193array(1) {
194  [0]=>
195  NULL
196}
197array(1) {
198  [0]=>
199  NULL
200}
201bool(false)
202
203-- iteration 12 --
204array(1) {
205  [0]=>
206  NULL
207}
208array(1) {
209  [0]=>
210  NULL
211}
212bool(false)
213
214-- iteration 13 --
215array(0) {
216}
217array(0) {
218}
219bool(false)
220
221*** Done ***
222