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