<?php
class file_operation{
var $file;
function file_operation($file){
$this->file = $file;
}
function write_int($value)
{
$data = pack('N', $value);
fwrite($this->file, $data);
}
function write_utf($value)
{
$this->write_int(strlen($value));
fwrite($this->file,$value,strlen($value));
}
function read_int()
{
$data = fread($this->file, 4);
$raw = unpack('N', $data);
$val = $raw[1] & 0xffffffff;
return $val;
}
function read_utf()
{
$l = $this->read_int();
return fread($this->file,$l);
}
}
$f = fopen("/home/gt/1.txt","wb");
$fo = new file_operation($f);
$fo->write_utf("abcdefg");
$fo->write_int(123456789);
$f2 = fopen("/home/gt/1.txt","rb");
$fo2 = new file_operation($f2);
echo $fo2->read_utf();
echo $fo2->read_int();
PHP 实现文件的writeInt writeUTF readInt readUTF
最新推荐文章于 2022-10-31 00:02:38 发布