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