1--TEST--
2Test fscanf() function: usage variations - char formats with boolean
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 boolean data using different char format types */
12
13$file_path = dirname(__FILE__);
14
15echo "*** Test fscanf(): different char format types with boolean data ***\n";
16
17// create a file
18$filename = "$file_path/fscanf_variation25.tmp";
19$file_handle = fopen($filename, "w");
20if($file_handle == false)
21  exit("Error:failed to open file $filename");
22
23// array of boolean types
24$bool_types = array (
25  true,
26  false,
27  TRUE,
28  FALSE,
29);
30
31$char_formats = array( "%c",
32		       "%hc", "%lc", "%Lc",
33		       " %c", "%c ", "% c",
34		       "\t%c", "\n%c", "%4c",
35		       "%30c", "%[a-zA-Z@#$&0-9]", "%*c");
36
37$counter = 1;
38
39// writing to the file
40foreach($bool_types as $value) {
41  @fprintf($file_handle, $value);
42  @fprintf($file_handle, "\n");
43}
44// closing the file
45fclose($file_handle);
46
47// opening the file for reading
48$file_handle = fopen($filename, "r");
49if($file_handle == false) {
50  exit("Error:failed to open file $filename");
51}
52
53$counter = 1;
54// reading the values from file using different char formats
55foreach($char_formats as $char_format) {
56  // rewind the file so that for every foreach iteration the file pointer starts from bof
57  rewind($file_handle);
58  echo "\n-- iteration $counter --\n";
59  while( !feof($file_handle) ) {
60    var_dump( fscanf($file_handle,$char_format) );
61  }
62  $counter++;
63}
64
65echo "\n*** Done ***";
66?>
67--CLEAN--
68<?php
69$file_path = dirname(__FILE__);
70$filename = "$file_path/fscanf_variation25.tmp";
71unlink($filename);
72?>
73--EXPECTF--
74*** Test fscanf(): different char format types with boolean data ***
75
76-- iteration 1 --
77array(1) {
78  [0]=>
79  string(1) "1"
80}
81array(1) {
82  [0]=>
83  string(0) ""
84}
85array(1) {
86  [0]=>
87  string(1) "1"
88}
89array(1) {
90  [0]=>
91  string(0) ""
92}
93bool(false)
94
95-- iteration 2 --
96array(1) {
97  [0]=>
98  string(1) "1"
99}
100array(1) {
101  [0]=>
102  string(0) ""
103}
104array(1) {
105  [0]=>
106  string(1) "1"
107}
108array(1) {
109  [0]=>
110  string(0) ""
111}
112bool(false)
113
114-- iteration 3 --
115array(1) {
116  [0]=>
117  string(1) "1"
118}
119array(1) {
120  [0]=>
121  string(0) ""
122}
123array(1) {
124  [0]=>
125  string(1) "1"
126}
127array(1) {
128  [0]=>
129  string(0) ""
130}
131bool(false)
132
133-- iteration 4 --
134array(1) {
135  [0]=>
136  string(1) "1"
137}
138array(1) {
139  [0]=>
140  string(0) ""
141}
142array(1) {
143  [0]=>
144  string(1) "1"
145}
146array(1) {
147  [0]=>
148  string(0) ""
149}
150bool(false)
151
152-- iteration 5 --
153array(1) {
154  [0]=>
155  string(1) "1"
156}
157NULL
158array(1) {
159  [0]=>
160  string(1) "1"
161}
162NULL
163bool(false)
164
165-- iteration 6 --
166array(1) {
167  [0]=>
168  string(1) "1"
169}
170array(1) {
171  [0]=>
172  string(0) ""
173}
174array(1) {
175  [0]=>
176  string(1) "1"
177}
178array(1) {
179  [0]=>
180  string(0) ""
181}
182bool(false)
183
184-- iteration 7 --
185
186Warning: fscanf(): Bad scan conversion character " " in %s on line %d
187NULL
188
189Warning: fscanf(): Bad scan conversion character " " in %s on line %d
190NULL
191
192Warning: fscanf(): Bad scan conversion character " " in %s on line %d
193NULL
194
195Warning: fscanf(): Bad scan conversion character " " in %s on line %d
196NULL
197bool(false)
198
199-- iteration 8 --
200array(1) {
201  [0]=>
202  string(1) "1"
203}
204NULL
205array(1) {
206  [0]=>
207  string(1) "1"
208}
209NULL
210bool(false)
211
212-- iteration 9 --
213array(1) {
214  [0]=>
215  string(1) "1"
216}
217NULL
218array(1) {
219  [0]=>
220  string(1) "1"
221}
222NULL
223bool(false)
224
225-- iteration 10 --
226array(1) {
227  [0]=>
228  string(1) "1"
229}
230array(1) {
231  [0]=>
232  string(0) ""
233}
234array(1) {
235  [0]=>
236  string(1) "1"
237}
238array(1) {
239  [0]=>
240  string(0) ""
241}
242bool(false)
243
244-- iteration 11 --
245array(1) {
246  [0]=>
247  string(1) "1"
248}
249array(1) {
250  [0]=>
251  string(0) ""
252}
253array(1) {
254  [0]=>
255  string(1) "1"
256}
257array(1) {
258  [0]=>
259  string(0) ""
260}
261bool(false)
262
263-- iteration 12 --
264array(1) {
265  [0]=>
266  string(1) "1"
267}
268array(1) {
269  [0]=>
270  NULL
271}
272array(1) {
273  [0]=>
274  string(1) "1"
275}
276array(1) {
277  [0]=>
278  NULL
279}
280bool(false)
281
282-- iteration 13 --
283array(0) {
284}
285array(0) {
286}
287array(0) {
288}
289array(0) {
290}
291bool(false)
292
293*** Done ***
294