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