php讀取二進(jìn)制流(C語言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
盡管php是用C語言開發(fā)的,不過令我不解的是php沒有提供對結(jié)構(gòu)體struct的直接支持。
不過php提供了pack和unpack函數(shù),用來進(jìn)行二進(jìn)制數(shù)據(jù)(binary data)和php內(nèi)部數(shù)據(jù)的互轉(zhuǎn):
string pack ( string $format [, mixed $args [, mixed $...]] )
//Pack given arguments into binary string according to format.
array unpack ( string $format, string $data )
//Unpacks from a binary string into an array according to the given format.
其中,$format跟perl里的pack格式類似,有如下一些(中文是我加的,有不準(zhǔn)確的歡迎提出):
a NUL-padded string,即“\0”作為“空字符”的表示形式
A SPACE-padded string,空格作為“空字符”的表示形式
h Hex string, low nibble first,升序位順序
H Hex string, high nibble first,降序位順序
c signed char,有符號單字節(jié)
C unsigned char,無符號單字節(jié)
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte,實際使用的時候作為跳過多少字節(jié)用,很有用
X Back up one byte,后退1字節(jié)
@ NUL-fill to absolute position,實際使用的時候作為從開頭跳到某字節(jié)用,很有用
實際使用發(fā)現(xiàn):C里的“\0”(即字符串終止符)在php里并不是終止符,而是作為了字符串的一部分。因此,必須對“\0”進(jìn)行特殊處理,才能進(jìn)行struct和php內(nèi)部數(shù)據(jù)的完美互轉(zhuǎn)。比如 char name[10]; 如果實際數(shù)據(jù)是“62 69 61 6E 00 62 69 616E00”,在C語言里第5個位置有終止符,name應(yīng)該是“bian”;而用了unpack轉(zhuǎn)換以后在php里的name卻是“bian\0bian\0”。
一開始我用了strpos函數(shù)找到“\0”的位置,然后進(jìn)行substr截取.
不過很Faint的事情發(fā)生了,不知道是strpos的bug還是substr的bug(其實測試一下就知道,懶得試),有些字符串沒問題,有些字符串卻只能得到空值(即$name == ”)。很是郁悶,后來找了個strtok函數(shù),這下沒有問題了.
難為大家看了那么多,下面寫個完整的php讀取二進(jìn)制數(shù)據(jù)流(C語言結(jié)構(gòu)體struct數(shù)據(jù))文件的示例代碼:
首先是C的struct定義示例,為了演示,我就寫個簡單點的,實際對照上面那個$format格式表應(yīng)該沒有問題:
struct BIANBIAN {
char name[10];
char pass[33];
int age;
unsigned char flag;
};
比如有個“file.dat”文件,內(nèi)容就是上面的N個BIANBIAN結(jié)構(gòu)體構(gòu)成的。讀取的php代碼:
<?php
//下面根據(jù)struct確定$format,注意int類型跟機器環(huán)境有關(guān),我的32位Linux是4個長度
$format = 'a10name/a33pass/iage/Cflag';
//確定一個struct占用多少長度字節(jié),如果只是讀取單個結(jié)構(gòu)體這是不需要的
$length = 10 + 33 + 4 + 1;
//也可以用fopen + fread + fclose,不過file_get_contents因為可以mmap,效率更高
$data = file_get_contents('file.dat', 'r');
for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
$bianbian = unpack("$format", $data);
//reference傳遞是php 5才支持的,如果用php4,得用其他辦法
foreach ($bianbian as &$value) {
if (is_string($value)) {
$value = strtok($value, "\0");
}
}
print_r($bianbian);
}
?>
pack應(yīng)該跟unpack相反。
順便附上生成結(jié)構(gòu)體文件的C語言代碼:
#include <stdio.h>
#include <string.h>
struct example
{
char name[10];
char pass[33];
int age;
unsigned char flag;
};
int main()
{
example test;
example read;
FILE *fp;
test.age = 111;
test.flag = 10;
strcpy(test.name, "Hello World!");
strcpy(test.pass, "zbl110119");
fp = fopen("file.dat", "w+");
if (!fp)
{
printf("open file error!");
return -1;
}
rewind(fp);
fwrite(&test, sizeof(example), 1, fp);
rewind(fp);
fread(&read, sizeof(example), 1, fp);
printf("%d, %s\n", read.age, read.name);
fclose(fp);
return 0;
}
- 深入分析C語言中結(jié)構(gòu)體指針的定義與引用詳解
- 淺談C語言中結(jié)構(gòu)體的初始化
- C語言中結(jié)構(gòu)體(struct)的幾種初始化方法
- C語言 結(jié)構(gòu)體(Struct)詳解及示例代碼
- C語言利用結(jié)構(gòu)體數(shù)組實現(xiàn)學(xué)生成績管理系統(tǒng)
- C語言結(jié)構(gòu)體(struct)常見使用方法(細(xì)節(jié)問題)
- C語言 結(jié)構(gòu)體和指針詳解及簡單示例
- C語言中結(jié)構(gòu)體struct編寫的一些要點解析
- 詳解C語言中結(jié)構(gòu)體的自引用和相互引用
- C語言結(jié)構(gòu)體的一些理解
相關(guān)文章
PHP 抽象方法與抽象類abstract關(guān)鍵字介紹及應(yīng)用
抽象方法指沒有方法體的方法,只要一個類里面有一個方法是抽象方法,那么這個類就要定義為抽象類,不了解的朋友可以看看2014-10-10IIS下PHP連接數(shù)據(jù)庫提示mysql undefined function mysql_connect()
在很多php教程初學(xué)者都會在初次php mysql時出來undefined function mysql_connect() 錯誤提示,下面我們來分析原因中。2010-06-06