hello, I'm now trying out file operations using PHP. I'm not sure abt this but they dun seen to be anyway similar to the functionality of C/C++ I/O functions..

if at all possible. can i just fread() binary data into an integer variable where by it is treated as numbers instead of string? i.e. $success=fread($fp,$longnumber,4). also if this does works. which byte order are we doing? IBM or Motorola? which come 1st, LSB or HSB?

noticed there's no 'read to buffer' in php's fread() but a return string >> too presumtuous that we are all dealing with strings only data.

Jem

Thanks if u can help. bye!

    1. you can cast explicitly with (int). this should work for binary data off fread(). additionally, an auto cast occurrs when you use a variable in an expression that requires a specific type. ie $foo="5" $foo=$foo+2 casts $foo to an int. php will always choose the narrowest data type that works.

    2. i believe the endian nature is defined by yr platform. so if you read that bin file on, say, a sparc, it will choose the appropriate endian and use that forever. i'm fairly sure that when you configure php it checks the endian and sets it there. that's just an educated guess though. you could actually test it out if you have a big and a small endian machine available.

    3. fread() should have all the features of the c's fread(). check the manual, but i'm pretty sure that the entirty of stdio is the same or really similar.

    -frymaster

      dude, it doesn't work that way.. i'd rather u speak out of something u've done before instead of speculating. the data is in BINARY, there's no 'auto convert' from binary greek to numbers.

      Anyway, I've done some work around by getting single byte values using ord() & shifting them to their proper place in the long number.

      Jem

        thanks for 'trying' to help, but dude, u are just showing me the manual which i've already read. give me something u've tested/done instead pls.

        reality check: unpack() gives array, not integral numbers of any form.

        bye

          this works fine for me... and i didn't even need an explicit cast.
          <?php
          // some data
          $testdata = "345657";

          // we shall write binary
          $fp = fopen("./bar.bin", "wb");

          // write and close
          fwrite($fp, $testdata, strlen($testdata));
          fclose($fp);

          // open to read
          $fp = fopen("./bar.bin", "rb");

          // read first two
          $int_1 = fread($fp, 1);
          $int_2 = fread($fp, 1);

          // add them to prove they're ints
          echo $int_1 + $int_2;

          ?>

            Ok. i will try not to be nasty here:

            #1. $testdata = "345657";
            will cast $testdata = string data.
            u can go "type bar.bin" in DOS and see the numbers exactly as 345657. Dun take my word for it. see for urself.

            Thus this $testdata dun stand valid for numeric binary testing. the rest of the testing is rendered useless as well.

            #2. IF you use $testdata = 345657;
            i.e. in binary representation it would be 0x054639 or (at least) 3 bytes (4 bytes for long int) of binary value.

            each of them in their byte representation would be (if u read off 1 by 1)
            0x05 =
            0x46 = ,
            0x39 = '

            at 1st glance, none of them look number (ascii). and surely after u fread() them, even if u shld (int)-cast them, they are garbage and will be 0-cast instead. Adding them returns 0 nonetheless.

            Jem

            Hope u have fun finding out the truth.

              dos? you're trying to make this difficult deliberately!

              mail me yr bin file. i won't be able to test under dos for you though.

                dude, where did u get the idea of me trying to make things difficult DELIBERATELY? it is a LIFE situation that i meet everyday/time. If u can't handle this then maybe u shld not try to act like u know everything and offer advise that ain't of much help at all.

                I've done my homework/testing and come to the conclusion in my original post. I'm just buffled at how far more PHP need to go to come close to an ANSI C standard functions. Maybe i shld write to the developers of PHP instead of posting here and get garbage inputs.

                The files I'm working on are 10mb-35mb in size. If u really wanna take this challenge seriously. I can try to make a small <500kb sample file with their datastructure specs for u. I won't blame u if u aren't the least interested. I'm very busy with my stuff. i can only do this 'personal' favor in the coming week.

                  Write a Reply...