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