1--TEST-- 2Test fscanf() function: usage variations - integer formats with strings 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 strings using different integer format types */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): different integer format types with strings ***\n"; 16 17// create a file 18$filename = "$file_path/fscanf_variation6.tmp"; 19$file_handle = fopen($filename, "w"); 20if($file_handle == false) 21 exit("Error:failed to open file $filename"); 22 23// array of strings 24$strings = array ( 25 "", 26 '', 27 "0", 28 '0', 29 "1", 30 '1', 31 "\x01", 32 '\x01', 33 "\01", 34 '\01', 35 'string', 36 "string", 37 "true", 38 "FALSE", 39 'false', 40 'TRUE', 41 "NULL", 42 'null' 43); 44 45$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d"); 46 47$counter = 1; 48 49// writing to the file 50foreach($strings as $string) { 51 @fprintf($file_handle, $string); 52 @fprintf($file_handle, "\n"); 53} 54// closing the file 55fclose($file_handle); 56 57// opening the file for reading 58$file_handle = fopen($filename, "r"); 59if($file_handle == false) { 60 exit("Error:failed to open file $filename"); 61} 62 63$counter = 1; 64// reading the values from file using different integer formats 65foreach($int_formats as $int_format) { 66 // rewind the file so that for every foreach iteration the file pointer starts from bof 67 rewind($file_handle); 68 echo "\n-- iteration $counter --\n"; 69 while( !feof($file_handle) ) { 70 var_dump( fscanf($file_handle,$int_format) ); 71 } 72 $counter++; 73} 74 75echo "\n*** Done ***"; 76?> 77--CLEAN-- 78<?php 79$file_path = dirname(__FILE__); 80$filename = "$file_path/fscanf_variation6.tmp"; 81unlink($filename); 82?> 83--EXPECTF-- 84*** Test fscanf(): different integer format types with strings *** 85 86-- iteration 1 -- 87NULL 88NULL 89array(1) { 90 [0]=> 91 int(0) 92} 93array(1) { 94 [0]=> 95 int(0) 96} 97array(1) { 98 [0]=> 99 int(1) 100} 101array(1) { 102 [0]=> 103 int(1) 104} 105array(1) { 106 [0]=> 107 NULL 108} 109array(1) { 110 [0]=> 111 NULL 112} 113array(1) { 114 [0]=> 115 NULL 116} 117array(1) { 118 [0]=> 119 NULL 120} 121array(1) { 122 [0]=> 123 NULL 124} 125array(1) { 126 [0]=> 127 NULL 128} 129array(1) { 130 [0]=> 131 NULL 132} 133array(1) { 134 [0]=> 135 NULL 136} 137array(1) { 138 [0]=> 139 NULL 140} 141array(1) { 142 [0]=> 143 NULL 144} 145array(1) { 146 [0]=> 147 NULL 148} 149array(1) { 150 [0]=> 151 NULL 152} 153bool(false) 154 155-- iteration 2 -- 156NULL 157NULL 158array(1) { 159 [0]=> 160 int(0) 161} 162array(1) { 163 [0]=> 164 int(0) 165} 166array(1) { 167 [0]=> 168 int(1) 169} 170array(1) { 171 [0]=> 172 int(1) 173} 174array(1) { 175 [0]=> 176 NULL 177} 178array(1) { 179 [0]=> 180 NULL 181} 182array(1) { 183 [0]=> 184 NULL 185} 186array(1) { 187 [0]=> 188 NULL 189} 190array(1) { 191 [0]=> 192 NULL 193} 194array(1) { 195 [0]=> 196 NULL 197} 198array(1) { 199 [0]=> 200 NULL 201} 202array(1) { 203 [0]=> 204 NULL 205} 206array(1) { 207 [0]=> 208 NULL 209} 210array(1) { 211 [0]=> 212 NULL 213} 214array(1) { 215 [0]=> 216 NULL 217} 218array(1) { 219 [0]=> 220 NULL 221} 222bool(false) 223 224-- iteration 3 -- 225NULL 226NULL 227array(1) { 228 [0]=> 229 int(0) 230} 231array(1) { 232 [0]=> 233 int(0) 234} 235array(1) { 236 [0]=> 237 int(1) 238} 239array(1) { 240 [0]=> 241 int(1) 242} 243array(1) { 244 [0]=> 245 NULL 246} 247array(1) { 248 [0]=> 249 NULL 250} 251array(1) { 252 [0]=> 253 NULL 254} 255array(1) { 256 [0]=> 257 NULL 258} 259array(1) { 260 [0]=> 261 NULL 262} 263array(1) { 264 [0]=> 265 NULL 266} 267array(1) { 268 [0]=> 269 NULL 270} 271array(1) { 272 [0]=> 273 NULL 274} 275array(1) { 276 [0]=> 277 NULL 278} 279array(1) { 280 [0]=> 281 NULL 282} 283array(1) { 284 [0]=> 285 NULL 286} 287array(1) { 288 [0]=> 289 NULL 290} 291bool(false) 292 293-- iteration 4 -- 294NULL 295NULL 296array(1) { 297 [0]=> 298 int(0) 299} 300array(1) { 301 [0]=> 302 int(0) 303} 304array(1) { 305 [0]=> 306 int(1) 307} 308array(1) { 309 [0]=> 310 int(1) 311} 312array(1) { 313 [0]=> 314 NULL 315} 316array(1) { 317 [0]=> 318 NULL 319} 320array(1) { 321 [0]=> 322 NULL 323} 324array(1) { 325 [0]=> 326 NULL 327} 328array(1) { 329 [0]=> 330 NULL 331} 332array(1) { 333 [0]=> 334 NULL 335} 336array(1) { 337 [0]=> 338 NULL 339} 340array(1) { 341 [0]=> 342 NULL 343} 344array(1) { 345 [0]=> 346 NULL 347} 348array(1) { 349 [0]=> 350 NULL 351} 352array(1) { 353 [0]=> 354 NULL 355} 356array(1) { 357 [0]=> 358 NULL 359} 360bool(false) 361 362-- iteration 5 -- 363NULL 364NULL 365array(1) { 366 [0]=> 367 int(0) 368} 369array(1) { 370 [0]=> 371 int(0) 372} 373array(1) { 374 [0]=> 375 int(1) 376} 377array(1) { 378 [0]=> 379 int(1) 380} 381array(1) { 382 [0]=> 383 NULL 384} 385array(1) { 386 [0]=> 387 NULL 388} 389array(1) { 390 [0]=> 391 NULL 392} 393array(1) { 394 [0]=> 395 NULL 396} 397array(1) { 398 [0]=> 399 NULL 400} 401array(1) { 402 [0]=> 403 NULL 404} 405array(1) { 406 [0]=> 407 NULL 408} 409array(1) { 410 [0]=> 411 NULL 412} 413array(1) { 414 [0]=> 415 NULL 416} 417array(1) { 418 [0]=> 419 NULL 420} 421array(1) { 422 [0]=> 423 NULL 424} 425array(1) { 426 [0]=> 427 NULL 428} 429bool(false) 430 431-- iteration 6 -- 432NULL 433NULL 434array(1) { 435 [0]=> 436 int(0) 437} 438array(1) { 439 [0]=> 440 int(0) 441} 442array(1) { 443 [0]=> 444 int(1) 445} 446array(1) { 447 [0]=> 448 int(1) 449} 450array(1) { 451 [0]=> 452 NULL 453} 454array(1) { 455 [0]=> 456 NULL 457} 458array(1) { 459 [0]=> 460 NULL 461} 462array(1) { 463 [0]=> 464 NULL 465} 466array(1) { 467 [0]=> 468 NULL 469} 470array(1) { 471 [0]=> 472 NULL 473} 474array(1) { 475 [0]=> 476 NULL 477} 478array(1) { 479 [0]=> 480 NULL 481} 482array(1) { 483 [0]=> 484 NULL 485} 486array(1) { 487 [0]=> 488 NULL 489} 490array(1) { 491 [0]=> 492 NULL 493} 494array(1) { 495 [0]=> 496 NULL 497} 498bool(false) 499 500-- iteration 7 -- 501 502Warning: fscanf(): Bad scan conversion character " " in %s on line %d 503NULL 504 505Warning: fscanf(): Bad scan conversion character " " in %s on line %d 506NULL 507 508Warning: fscanf(): Bad scan conversion character " " in %s on line %d 509NULL 510 511Warning: fscanf(): Bad scan conversion character " " in %s on line %d 512NULL 513 514Warning: fscanf(): Bad scan conversion character " " in %s on line %d 515NULL 516 517Warning: fscanf(): Bad scan conversion character " " in %s on line %d 518NULL 519 520Warning: fscanf(): Bad scan conversion character " " in %s on line %d 521NULL 522 523Warning: fscanf(): Bad scan conversion character " " in %s on line %d 524NULL 525 526Warning: fscanf(): Bad scan conversion character " " in %s on line %d 527NULL 528 529Warning: fscanf(): Bad scan conversion character " " in %s on line %d 530NULL 531 532Warning: fscanf(): Bad scan conversion character " " in %s on line %d 533NULL 534 535Warning: fscanf(): Bad scan conversion character " " in %s on line %d 536NULL 537 538Warning: fscanf(): Bad scan conversion character " " in %s on line %d 539NULL 540 541Warning: fscanf(): Bad scan conversion character " " in %s on line %d 542NULL 543 544Warning: fscanf(): Bad scan conversion character " " in %s on line %d 545NULL 546 547Warning: fscanf(): Bad scan conversion character " " in %s on line %d 548NULL 549 550Warning: fscanf(): Bad scan conversion character " " in %s on line %d 551NULL 552 553Warning: fscanf(): Bad scan conversion character " " in %s on line %d 554NULL 555bool(false) 556 557-- iteration 8 -- 558NULL 559NULL 560array(1) { 561 [0]=> 562 int(0) 563} 564array(1) { 565 [0]=> 566 int(0) 567} 568array(1) { 569 [0]=> 570 int(1) 571} 572array(1) { 573 [0]=> 574 int(1) 575} 576array(1) { 577 [0]=> 578 NULL 579} 580array(1) { 581 [0]=> 582 NULL 583} 584array(1) { 585 [0]=> 586 NULL 587} 588array(1) { 589 [0]=> 590 NULL 591} 592array(1) { 593 [0]=> 594 NULL 595} 596array(1) { 597 [0]=> 598 NULL 599} 600array(1) { 601 [0]=> 602 NULL 603} 604array(1) { 605 [0]=> 606 NULL 607} 608array(1) { 609 [0]=> 610 NULL 611} 612array(1) { 613 [0]=> 614 NULL 615} 616array(1) { 617 [0]=> 618 NULL 619} 620array(1) { 621 [0]=> 622 NULL 623} 624bool(false) 625 626-- iteration 9 -- 627NULL 628NULL 629array(1) { 630 [0]=> 631 int(0) 632} 633array(1) { 634 [0]=> 635 int(0) 636} 637array(1) { 638 [0]=> 639 int(1) 640} 641array(1) { 642 [0]=> 643 int(1) 644} 645array(1) { 646 [0]=> 647 NULL 648} 649array(1) { 650 [0]=> 651 NULL 652} 653array(1) { 654 [0]=> 655 NULL 656} 657array(1) { 658 [0]=> 659 NULL 660} 661array(1) { 662 [0]=> 663 NULL 664} 665array(1) { 666 [0]=> 667 NULL 668} 669array(1) { 670 [0]=> 671 NULL 672} 673array(1) { 674 [0]=> 675 NULL 676} 677array(1) { 678 [0]=> 679 NULL 680} 681array(1) { 682 [0]=> 683 NULL 684} 685array(1) { 686 [0]=> 687 NULL 688} 689array(1) { 690 [0]=> 691 NULL 692} 693bool(false) 694 695-- iteration 10 -- 696NULL 697NULL 698array(1) { 699 [0]=> 700 int(0) 701} 702array(1) { 703 [0]=> 704 int(0) 705} 706array(1) { 707 [0]=> 708 int(1) 709} 710array(1) { 711 [0]=> 712 int(1) 713} 714array(1) { 715 [0]=> 716 NULL 717} 718array(1) { 719 [0]=> 720 NULL 721} 722array(1) { 723 [0]=> 724 NULL 725} 726array(1) { 727 [0]=> 728 NULL 729} 730array(1) { 731 [0]=> 732 NULL 733} 734array(1) { 735 [0]=> 736 NULL 737} 738array(1) { 739 [0]=> 740 NULL 741} 742array(1) { 743 [0]=> 744 NULL 745} 746array(1) { 747 [0]=> 748 NULL 749} 750array(1) { 751 [0]=> 752 NULL 753} 754array(1) { 755 [0]=> 756 NULL 757} 758array(1) { 759 [0]=> 760 NULL 761} 762bool(false) 763 764-- iteration 11 -- 765NULL 766NULL 767array(1) { 768 [0]=> 769 int(0) 770} 771array(1) { 772 [0]=> 773 int(0) 774} 775array(1) { 776 [0]=> 777 int(1) 778} 779array(1) { 780 [0]=> 781 int(1) 782} 783array(1) { 784 [0]=> 785 NULL 786} 787array(1) { 788 [0]=> 789 NULL 790} 791array(1) { 792 [0]=> 793 NULL 794} 795array(1) { 796 [0]=> 797 NULL 798} 799array(1) { 800 [0]=> 801 NULL 802} 803array(1) { 804 [0]=> 805 NULL 806} 807array(1) { 808 [0]=> 809 NULL 810} 811array(1) { 812 [0]=> 813 NULL 814} 815array(1) { 816 [0]=> 817 NULL 818} 819array(1) { 820 [0]=> 821 NULL 822} 823array(1) { 824 [0]=> 825 NULL 826} 827array(1) { 828 [0]=> 829 NULL 830} 831bool(false) 832 833-- iteration 12 -- 834array(1) { 835 [0]=> 836 NULL 837} 838array(1) { 839 [0]=> 840 NULL 841} 842array(1) { 843 [0]=> 844 string(1) "0" 845} 846array(1) { 847 [0]=> 848 string(1) "0" 849} 850array(1) { 851 [0]=> 852 string(1) "1" 853} 854array(1) { 855 [0]=> 856 string(1) "1" 857} 858array(1) { 859 [0]=> 860 NULL 861} 862array(1) { 863 [0]=> 864 NULL 865} 866array(1) { 867 [0]=> 868 NULL 869} 870array(1) { 871 [0]=> 872 NULL 873} 874array(1) { 875 [0]=> 876 NULL 877} 878array(1) { 879 [0]=> 880 NULL 881} 882array(1) { 883 [0]=> 884 NULL 885} 886array(1) { 887 [0]=> 888 NULL 889} 890array(1) { 891 [0]=> 892 NULL 893} 894array(1) { 895 [0]=> 896 NULL 897} 898array(1) { 899 [0]=> 900 NULL 901} 902array(1) { 903 [0]=> 904 NULL 905} 906bool(false) 907 908-- iteration 13 -- 909NULL 910NULL 911array(0) { 912} 913array(0) { 914} 915array(0) { 916} 917array(0) { 918} 919array(0) { 920} 921array(0) { 922} 923array(0) { 924} 925array(0) { 926} 927array(0) { 928} 929array(0) { 930} 931array(0) { 932} 933array(0) { 934} 935array(0) { 936} 937array(0) { 938} 939array(0) { 940} 941array(0) { 942} 943bool(false) 944 945*** Done *** 946