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