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