Reputation: 2807
I have created binary file using C codings. This is the structure of that binary file.
struct emp
{
int eid,eage;
char name[20],city[20];
}record;
Using this 'C' Structure i created a binary file called "table1.txt"
Now i want to show the contents of the file in a web page using php. How can i do this ?
<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>
I have used the above code. But i can only read the first field of the file only. My first Record field is Employee id its values is '0'. The page displays only 0.
Upvotes: 4
Views: 3237
Reputation: 2807
This is 'C' Structure. struct gross { char date[11]; char ac[128]; char type[5]; float mvalue; float netraw; float netfer; char stat[128]; float firr; float acb; };
This is the content of binary file when compiled with GCC compiler.
12/12/1995 d us 12.23 34.12 90.12 fees 12 56.12 01/01/1998 a us 52.23 54.12 10.12 fees 92 16.12 31/12/1999 a us 52.23 54.12 10.12 fees 92 16.12 31/12/1999 d us 12.23 34.12 90.12 fees 12 56.12 01/01/2000 a us 52.23 54.12 10.12 fees 92 16.12 01/01/2000 z us 12.23 34.12 90.12 fees 12 56.12 31/12/2010 a us 52.23 54.12 10.12 fees 92 16.12 31/12/2010 d us 12.23 34.12 90.12 fees 12 56.12
This is the PHP coding to read the content of the above binary file.
echo "<table>";
while (!feof($f)) {
if ($s = fread($f, 292)) {
$nn = unpack('a11date/a128ac/a5type/fmvalue/fnetraw/fnetfer/a128stat/ffirr/facb', $s);
echo "<td>" . $nn[date] ."</td>";
echo "<td>" . $nn[ac] . "</td>";
echo "<td>" . $nn[type] . "</td>";
echo "<td>" . $nn[mvalue] . "</td>";
echo "<td>" . $nn[netraw] . "</td>";
echo "<td>" . $nn[netfer] . "</td>";
echo "<td>" . $nn[stat] . "</td>";
echo "<td>" . $nn[firr] . "</td>";
echo "<td>" . $nn[acb] . "</td>";
echo "</tr>";
}
}
echo "</table>";
fclose($f);
?>
This is the answer i got from the above code. I'm getting lot of garbage values is second and seventh field. And How to set precision for the float fields.
12/12/1995 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.119998931885
01/01/1998 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.120000839233
31/12/1999 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.120000839233
31/12/1999 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.119998931885
01/01/2000 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.120000839233
01/01/2000 z¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.119998931885
31/12/2010 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.120000839233
31/12/2010 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg· X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.119998931885
Upvotes: 1
Reputation: 173552
For some strange reason, each data segment is not 48 bytes as expected but 52 bytes.
$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);
Upvotes: 1