tags:

views:

46

answers:

6

I have bytes file. Now need to read it by one bytes. How i can make that?

Its possible read array and remove readed element?

+1  A: 

Assuming you're reading from the front of the array, you want array_shift. You can read more at http://us2.php.net/manual/en/function.array-shift.php .

Do note that array_shift is linear in the size of the array, not constant time. If you really must follow this model, you might want to reverse the array first and use array_pop instead.

jemfinch
+1  A: 

Open the file for reading with fopen() and use fread() with a length of 1

Michael Mrozek
+1  A: 

You could use the fgetc function, to read one character/byte from a file -- and calling it in a loop.

Quoting the given example :

$fp = fopen('somefile.txt', 'r');
if (!$fp) {
    echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
    echo "$char\n";
}


This will work as PHP considers that one character == one byte (which is not always true... but that's another problem ^^ )

Pascal MARTIN
A: 

array_shift() will remove the first element of an array.

Reading from a file is usually done with strings though, in which case you should store the index of the character being worked on, and advance it when you've handled the current character.

Ignacio Vazquez-Abrams
A: 

Open the file with file_get_contents and read byte by byte with unpack.

Mark Tomlin
A: 

To read only one byte of a file with php:

$fh = fopen('mytest.txt', "r");
$contents = fread($fh, 1);
ArneRie