1--TEST--
2Test fgets() function : usage variations - seek n read
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) != 'WIN') {
6    die('skip only valid for Windows');
7}
8?>
9--FILE--
10<?php
11/*
12 Prototype: string fgets ( resource $handle [, int $length] );
13 Description: Gets a line from file pointer
14*/
15
16// include the file.inc for common test funcitons
17include ("file.inc");
18
19$file_modes = array("w+", "w+b", "w+t",
20                    "a+", "a+b", "a+t",
21                    "x+", "x+b", "x+t");
22
23$file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric");
24
25echo "*** Testing fgets() : usage variations ***\n";
26
27$filename = __DIR__."/fgets_variation4.tmp";
28
29foreach($file_modes as $file_mode) {
30  echo "\n-- Testing fgets() with file opened using mode $file_mode --\n";
31
32  foreach($file_content_types as $file_content_type) {
33    echo "-- File content type : $file_content_type --\n";
34
35    /* create files with $file_content_type */
36    $file_handle = fopen($filename, $file_mode);
37    $data = fill_file($file_handle, $file_content_type, 50);
38
39    if ( !$file_handle ) {
40      echo "Error: failed to open file $filename!";
41      exit();
42    }
43
44    echo "-- fgets() with location set by fseek() with default length --\n";
45    var_dump( fseek($file_handle, 5, SEEK_SET) );
46    var_dump( ftell($file_handle) );
47    var_dump( fgets($file_handle ) );
48    var_dump( ftell($file_handle) ); // ensure the file pointer position
49    var_dump( feof($file_handle) );  // enusre if eof set
50
51    echo "-- fgets() with location set by fseek() with length = 20 --\n";
52    var_dump( fseek($file_handle, 25, SEEK_SET) );
53    var_dump( ftell($file_handle) );
54    var_dump( fgets($file_handle, 20 ) ); // expected 19 chars
55    var_dump( ftell($file_handle) ); // ensure the file pointer position
56    var_dump( feof($file_handle) );  // enusre if eof set
57
58    //close file
59    fclose($file_handle);
60
61    // delete file
62    delete_file($filename);
63  } // file_content_type loop
64} // file_mode loop
65
66echo "Done\n";
67?>
68--EXPECT--
69*** Testing fgets() : usage variations ***
70
71-- Testing fgets() with file opened using mode w+ --
72-- File content type : numeric --
73-- fgets() with location set by fseek() with default length --
74int(0)
75int(5)
76string(45) "222222222222222222222222222222222222222222222"
77int(50)
78bool(true)
79-- fgets() with location set by fseek() with length = 20 --
80int(0)
81int(25)
82string(19) "2222222222222222222"
83int(44)
84bool(false)
85-- File content type : text --
86-- fgets() with location set by fseek() with default length --
87int(0)
88int(5)
89string(45) "text text text text text text text text text "
90int(50)
91bool(true)
92-- fgets() with location set by fseek() with length = 20 --
93int(0)
94int(25)
95string(19) "text text text text"
96int(44)
97bool(false)
98-- File content type : text_with_new_line --
99-- fgets() with location set by fseek() with default length --
100int(0)
101int(5)
102string(13) "line of text
103"
104int(18)
105bool(false)
106-- fgets() with location set by fseek() with length = 20 --
107int(0)
108int(25)
109string(11) "ne of text
110"
111int(36)
112bool(false)
113-- File content type : alphanumeric --
114-- fgets() with location set by fseek() with default length --
115int(0)
116int(5)
117string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
118int(50)
119bool(true)
120-- fgets() with location set by fseek() with length = 20 --
121int(0)
122int(25)
123string(19) "ab12 ab12 ab12 ab12"
124int(44)
125bool(false)
126
127-- Testing fgets() with file opened using mode w+b --
128-- File content type : numeric --
129-- fgets() with location set by fseek() with default length --
130int(0)
131int(5)
132string(45) "222222222222222222222222222222222222222222222"
133int(50)
134bool(true)
135-- fgets() with location set by fseek() with length = 20 --
136int(0)
137int(25)
138string(19) "2222222222222222222"
139int(44)
140bool(false)
141-- File content type : text --
142-- fgets() with location set by fseek() with default length --
143int(0)
144int(5)
145string(45) "text text text text text text text text text "
146int(50)
147bool(true)
148-- fgets() with location set by fseek() with length = 20 --
149int(0)
150int(25)
151string(19) "text text text text"
152int(44)
153bool(false)
154-- File content type : text_with_new_line --
155-- fgets() with location set by fseek() with default length --
156int(0)
157int(5)
158string(13) "line of text
159"
160int(18)
161bool(false)
162-- fgets() with location set by fseek() with length = 20 --
163int(0)
164int(25)
165string(11) "ne of text
166"
167int(36)
168bool(false)
169-- File content type : alphanumeric --
170-- fgets() with location set by fseek() with default length --
171int(0)
172int(5)
173string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
174int(50)
175bool(true)
176-- fgets() with location set by fseek() with length = 20 --
177int(0)
178int(25)
179string(19) "ab12 ab12 ab12 ab12"
180int(44)
181bool(false)
182
183-- Testing fgets() with file opened using mode w+t --
184-- File content type : numeric --
185-- fgets() with location set by fseek() with default length --
186int(0)
187int(5)
188string(45) "222222222222222222222222222222222222222222222"
189int(50)
190bool(true)
191-- fgets() with location set by fseek() with length = 20 --
192int(0)
193int(25)
194string(19) "2222222222222222222"
195int(44)
196bool(false)
197-- File content type : text --
198-- fgets() with location set by fseek() with default length --
199int(0)
200int(5)
201string(45) "text text text text text text text text text "
202int(50)
203bool(true)
204-- fgets() with location set by fseek() with length = 20 --
205int(0)
206int(25)
207string(19) "text text text text"
208int(44)
209bool(false)
210-- File content type : text_with_new_line --
211-- fgets() with location set by fseek() with default length --
212int(0)
213int(5)
214string(1) "
215"
216int(6)
217bool(false)
218-- fgets() with location set by fseek() with length = 20 --
219int(0)
220int(25)
221string(12) "ine of text
222"
223int(37)
224bool(false)
225-- File content type : alphanumeric --
226-- fgets() with location set by fseek() with default length --
227int(0)
228int(5)
229string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
230int(50)
231bool(true)
232-- fgets() with location set by fseek() with length = 20 --
233int(0)
234int(25)
235string(19) "ab12 ab12 ab12 ab12"
236int(44)
237bool(false)
238
239-- Testing fgets() with file opened using mode a+ --
240-- File content type : numeric --
241-- fgets() with location set by fseek() with default length --
242int(0)
243int(5)
244string(45) "222222222222222222222222222222222222222222222"
245int(50)
246bool(true)
247-- fgets() with location set by fseek() with length = 20 --
248int(0)
249int(25)
250string(19) "2222222222222222222"
251int(44)
252bool(false)
253-- File content type : text --
254-- fgets() with location set by fseek() with default length --
255int(0)
256int(5)
257string(45) "text text text text text text text text text "
258int(50)
259bool(true)
260-- fgets() with location set by fseek() with length = 20 --
261int(0)
262int(25)
263string(19) "text text text text"
264int(44)
265bool(false)
266-- File content type : text_with_new_line --
267-- fgets() with location set by fseek() with default length --
268int(0)
269int(5)
270string(13) "line of text
271"
272int(18)
273bool(false)
274-- fgets() with location set by fseek() with length = 20 --
275int(0)
276int(25)
277string(11) "ne of text
278"
279int(36)
280bool(false)
281-- File content type : alphanumeric --
282-- fgets() with location set by fseek() with default length --
283int(0)
284int(5)
285string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
286int(50)
287bool(true)
288-- fgets() with location set by fseek() with length = 20 --
289int(0)
290int(25)
291string(19) "ab12 ab12 ab12 ab12"
292int(44)
293bool(false)
294
295-- Testing fgets() with file opened using mode a+b --
296-- File content type : numeric --
297-- fgets() with location set by fseek() with default length --
298int(0)
299int(5)
300string(45) "222222222222222222222222222222222222222222222"
301int(50)
302bool(true)
303-- fgets() with location set by fseek() with length = 20 --
304int(0)
305int(25)
306string(19) "2222222222222222222"
307int(44)
308bool(false)
309-- File content type : text --
310-- fgets() with location set by fseek() with default length --
311int(0)
312int(5)
313string(45) "text text text text text text text text text "
314int(50)
315bool(true)
316-- fgets() with location set by fseek() with length = 20 --
317int(0)
318int(25)
319string(19) "text text text text"
320int(44)
321bool(false)
322-- File content type : text_with_new_line --
323-- fgets() with location set by fseek() with default length --
324int(0)
325int(5)
326string(13) "line of text
327"
328int(18)
329bool(false)
330-- fgets() with location set by fseek() with length = 20 --
331int(0)
332int(25)
333string(11) "ne of text
334"
335int(36)
336bool(false)
337-- File content type : alphanumeric --
338-- fgets() with location set by fseek() with default length --
339int(0)
340int(5)
341string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
342int(50)
343bool(true)
344-- fgets() with location set by fseek() with length = 20 --
345int(0)
346int(25)
347string(19) "ab12 ab12 ab12 ab12"
348int(44)
349bool(false)
350
351-- Testing fgets() with file opened using mode a+t --
352-- File content type : numeric --
353-- fgets() with location set by fseek() with default length --
354int(0)
355int(5)
356string(45) "222222222222222222222222222222222222222222222"
357int(50)
358bool(true)
359-- fgets() with location set by fseek() with length = 20 --
360int(0)
361int(25)
362string(19) "2222222222222222222"
363int(44)
364bool(false)
365-- File content type : text --
366-- fgets() with location set by fseek() with default length --
367int(0)
368int(5)
369string(45) "text text text text text text text text text "
370int(50)
371bool(true)
372-- fgets() with location set by fseek() with length = 20 --
373int(0)
374int(25)
375string(19) "text text text text"
376int(44)
377bool(false)
378-- File content type : text_with_new_line --
379-- fgets() with location set by fseek() with default length --
380int(0)
381int(5)
382string(1) "
383"
384int(6)
385bool(false)
386-- fgets() with location set by fseek() with length = 20 --
387int(0)
388int(25)
389string(12) "ine of text
390"
391int(37)
392bool(false)
393-- File content type : alphanumeric --
394-- fgets() with location set by fseek() with default length --
395int(0)
396int(5)
397string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
398int(50)
399bool(true)
400-- fgets() with location set by fseek() with length = 20 --
401int(0)
402int(25)
403string(19) "ab12 ab12 ab12 ab12"
404int(44)
405bool(false)
406
407-- Testing fgets() with file opened using mode x+ --
408-- File content type : numeric --
409-- fgets() with location set by fseek() with default length --
410int(0)
411int(5)
412string(45) "222222222222222222222222222222222222222222222"
413int(50)
414bool(true)
415-- fgets() with location set by fseek() with length = 20 --
416int(0)
417int(25)
418string(19) "2222222222222222222"
419int(44)
420bool(false)
421-- File content type : text --
422-- fgets() with location set by fseek() with default length --
423int(0)
424int(5)
425string(45) "text text text text text text text text text "
426int(50)
427bool(true)
428-- fgets() with location set by fseek() with length = 20 --
429int(0)
430int(25)
431string(19) "text text text text"
432int(44)
433bool(false)
434-- File content type : text_with_new_line --
435-- fgets() with location set by fseek() with default length --
436int(0)
437int(5)
438string(13) "line of text
439"
440int(18)
441bool(false)
442-- fgets() with location set by fseek() with length = 20 --
443int(0)
444int(25)
445string(11) "ne of text
446"
447int(36)
448bool(false)
449-- File content type : alphanumeric --
450-- fgets() with location set by fseek() with default length --
451int(0)
452int(5)
453string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
454int(50)
455bool(true)
456-- fgets() with location set by fseek() with length = 20 --
457int(0)
458int(25)
459string(19) "ab12 ab12 ab12 ab12"
460int(44)
461bool(false)
462
463-- Testing fgets() with file opened using mode x+b --
464-- File content type : numeric --
465-- fgets() with location set by fseek() with default length --
466int(0)
467int(5)
468string(45) "222222222222222222222222222222222222222222222"
469int(50)
470bool(true)
471-- fgets() with location set by fseek() with length = 20 --
472int(0)
473int(25)
474string(19) "2222222222222222222"
475int(44)
476bool(false)
477-- File content type : text --
478-- fgets() with location set by fseek() with default length --
479int(0)
480int(5)
481string(45) "text text text text text text text text text "
482int(50)
483bool(true)
484-- fgets() with location set by fseek() with length = 20 --
485int(0)
486int(25)
487string(19) "text text text text"
488int(44)
489bool(false)
490-- File content type : text_with_new_line --
491-- fgets() with location set by fseek() with default length --
492int(0)
493int(5)
494string(13) "line of text
495"
496int(18)
497bool(false)
498-- fgets() with location set by fseek() with length = 20 --
499int(0)
500int(25)
501string(11) "ne of text
502"
503int(36)
504bool(false)
505-- File content type : alphanumeric --
506-- fgets() with location set by fseek() with default length --
507int(0)
508int(5)
509string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
510int(50)
511bool(true)
512-- fgets() with location set by fseek() with length = 20 --
513int(0)
514int(25)
515string(19) "ab12 ab12 ab12 ab12"
516int(44)
517bool(false)
518
519-- Testing fgets() with file opened using mode x+t --
520-- File content type : numeric --
521-- fgets() with location set by fseek() with default length --
522int(0)
523int(5)
524string(45) "222222222222222222222222222222222222222222222"
525int(50)
526bool(true)
527-- fgets() with location set by fseek() with length = 20 --
528int(0)
529int(25)
530string(19) "2222222222222222222"
531int(44)
532bool(false)
533-- File content type : text --
534-- fgets() with location set by fseek() with default length --
535int(0)
536int(5)
537string(45) "text text text text text text text text text "
538int(50)
539bool(true)
540-- fgets() with location set by fseek() with length = 20 --
541int(0)
542int(25)
543string(19) "text text text text"
544int(44)
545bool(false)
546-- File content type : text_with_new_line --
547-- fgets() with location set by fseek() with default length --
548int(0)
549int(5)
550string(1) "
551"
552int(6)
553bool(false)
554-- fgets() with location set by fseek() with length = 20 --
555int(0)
556int(25)
557string(12) "ine of text
558"
559int(37)
560bool(false)
561-- File content type : alphanumeric --
562-- fgets() with location set by fseek() with default length --
563int(0)
564int(5)
565string(45) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
566int(50)
567bool(true)
568-- fgets() with location set by fseek() with length = 20 --
569int(0)
570int(25)
571string(19) "ab12 ab12 ab12 ab12"
572int(44)
573bool(false)
574Done
575