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