php(或c ++)如何从二进制文件中读取多种格式的数据

时间:2015-02-22 16:45:10

标签: php c++ binary

我有一个问题,我无法找到答案 我使用以下代码将数据从数据库写入二进制文件:

$data = array();
$prev = 1;
$i=0;

$query="select wc.MAC, ws.SIGNAL_STRENGTH, ws.ID, ws.USER_SCAN from wifi_cell as wc, wifi_scan as ws where ws.WIFI_CELL=wc.ID";
$result=kmysql_query($query);
while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) {
    $data[] = array(0,0,0,null,$row['MAC'],$row['SIGNAL_STRENGTH'],null,$row['USER_SCAN']);
    $i++;
}

mysql_free_result($result);

$j=0;
$query="select ul.LATITUDE, ul.LONGITUDE,ul.ALTITUDE, ul.TIME from user_location as ul where 1";
$result=kmysql_query($query);
while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) {
    while ($j<$i) {
        if ($data[$j][7] == $prev) {
            $data[$j][0] = $row['LATITUDE'];
            $data[$j][1] = $row['LONGITUDE'];
            $data[$j][2] = $row['ALTITUDE'];
            $data[$j][3] = $row['TIME'];
            $j++;
        }
        else {
            $prev = $data[$j][7];
            break;
        }

    }
}

mysql_free_result($result);

$handle = fopen('C:\\Users\\Duc Nguyen\\Desktop\\text1.bin', 'r+');
ftruncate($handle, 0);
fclose($handle);

for ($j=0;$j<$i;$j++) {
    $data[$j][0] = pack("d*",$data[$j][0]);
    $data[$j][1] = pack("d*",$data[$j][1]);
    $data[$j][2] = pack("d*",$data[$j][2]);
    $data[$j][3] = pack("A*",$data[$j][3]);
    $data[$j][4] = pack("Z*",$data[$j][4]);
    $data[$j][5] = pack("d*",$data[$j][5]);
    $data[$j][6] = pack("A*",$data[$j][6]);
    $data[$j][7] = pack("d*",$data[$j][7]);

    file_put_contents('C:\\Users\\Duc Nguyen\\Desktop\\text1.bin', $data[$j], FILE_APPEND);
}

但现在二进制文件包含许多类型的数据,我不知道如何阅读它。你能告诉我如何在php中阅读它们吗?还有c ++?

0 个答案:

没有答案