]> granicus.if.org Git - php/blob
f657c07005
[php] /
1 --TEST--
2 Test 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
15 echo "*** 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");
20 if($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
44 foreach($resource_types as $value) {
45   @fprintf($file_handle, "%s", $value);
46   @fprintf($file_handle, "\n");
47 }
48 // closing the file
49 fclose($file_handle);
50
51 // opening the file for reading
52 $file_handle = fopen($filename, "r");
53 if($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
59 foreach($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     try {
65       var_dump( fscanf($file_handle,$float_format) );
66     } catch (ValueError $exception) {
67       echo $exception->getMessage() . "\n";
68     }
69   }
70   $counter++;
71 }
72
73 // closing the resources
74 fclose($fp);
75 closedir($dfp);
76
77 echo "\n*** Done ***";
78 ?>
79 --CLEAN--
80 <?php
81 $file_path = __DIR__;
82 $filename = "$file_path/fscanf_variation10.tmp";
83 unlink($filename);
84 ?>
85 --EXPECTF--
86 *** Test fscanf(): different float format types with resource ***
87
88 -- iteration 1 --
89 array(1) {
90   [0]=>
91   NULL
92 }
93 array(1) {
94   [0]=>
95   NULL
96 }
97 bool(false)
98
99 -- iteration 2 --
100 array(1) {
101   [0]=>
102   NULL
103 }
104 array(1) {
105   [0]=>
106   NULL
107 }
108 bool(false)
109
110 -- iteration 3 --
111 array(1) {
112   [0]=>
113   NULL
114 }
115 array(1) {
116   [0]=>
117   NULL
118 }
119 bool(false)
120
121 -- iteration 4 --
122 array(1) {
123   [0]=>
124   NULL
125 }
126 array(1) {
127   [0]=>
128   NULL
129 }
130 bool(false)
131
132 -- iteration 5 --
133 array(1) {
134   [0]=>
135   NULL
136 }
137 array(1) {
138   [0]=>
139   NULL
140 }
141 bool(false)
142
143 -- iteration 6 --
144 array(1) {
145   [0]=>
146   NULL
147 }
148 array(1) {
149   [0]=>
150   NULL
151 }
152 bool(false)
153
154 -- iteration 7 --
155 Bad scan conversion character " "
156 Bad scan conversion character " "
157 bool(false)
158
159 -- iteration 8 --
160 array(1) {
161   [0]=>
162   NULL
163 }
164 array(1) {
165   [0]=>
166   NULL
167 }
168 bool(false)
169
170 -- iteration 9 --
171 array(1) {
172   [0]=>
173   NULL
174 }
175 array(1) {
176   [0]=>
177   NULL
178 }
179 bool(false)
180
181 -- iteration 10 --
182 array(1) {
183   [0]=>
184   NULL
185 }
186 array(1) {
187   [0]=>
188   NULL
189 }
190 bool(false)
191
192 -- iteration 11 --
193 array(1) {
194   [0]=>
195   NULL
196 }
197 array(1) {
198   [0]=>
199   NULL
200 }
201 bool(false)
202
203 -- iteration 12 --
204 array(1) {
205   [0]=>
206   NULL
207 }
208 array(1) {
209   [0]=>
210   NULL
211 }
212 bool(false)
213
214 -- iteration 13 --
215 array(0) {
216 }
217 array(0) {
218 }
219 bool(false)
220
221 *** Done ***