在php中读取二进制文件记录
发布时间:2020-12-13 16:16:40 所属栏目:PHP教程 来源:网络整理
导读:我使用C编码创建了二进制文件.这是该二进制文件的结构. struct emp{ int eid,eage; char name[20],city[20];}record; 使用这个’C’结构我创建了一个名为“table1.txt”的二进制文件 现在我想使用php在网页中显示文件的内容.我怎样才能做到这一点 ? htmlhea
我使用C编码创建了二进制文件.这是该二进制文件的结构.
struct emp { int eid,eage; char name[20],city[20]; }record; 使用这个’C’结构我创建了一个名为“table1.txt”的二进制文件 现在我想使用php在网页中显示文件的内容.我怎样才能做到这一点 ? <html> <head> <title>binary file</title></head> <body style="background-color:yellow"> <? $fp = fopen("table1.txt","rb"); $read = fread($fp,4); $n = unpack("i",$read); $data1 = fread($fp,8); $nn = unpack("i",$data1); echo $number[1]; ?> </body> </html> 我使用了上面的代码.但我只能读取文件的第一个字段.我的第一个Record字段是Employee id,其值为’0′.该页面仅显示0. 解决方法
由于某些奇怪的原因,每个数据段不是预期的48个字节,而是52个字节.
$f = fopen('data.txt','rb'); while (!feof($f)) { // read one segment of 52 bytes if ($s = fread($f,52)) { // unpack the binary structure into an associative array print_r(unpack('ieid/ieage/a20name/a20city',$s)); } } fclose($f); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |