开发者

array read by one element

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

开发者_StackOverflow

Its possible read array and remove readed element?


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.


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


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 ^^ )


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.


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


To read only one byte of a file with php:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜