gpt4 book ai didi

php - 在php中读取二进制文件记录

转载 作者:可可西里 更新时间:2023-10-31 23:33:24 24 4
gpt4 key购买 nike

我已经使用 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>

我已经使用了上面的代码。但我只能读取文件的第一个字段。我的第一个记录字段是员工 ​​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);

关于php - 在php中读取二进制文件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10895951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com