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