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