4

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.

4
  • Could you post the contents of bin2hex(file_get_contents('table1.txt'));? Commented Jun 5, 2012 at 10:53
  • 0 25 xxxxx yyyyy 1 32 x1x1x yyyyy 2 33 x2x2x yyyyy Note : There is no space or null characters between the fields. Commented Jun 5, 2012 at 11:03
  • That doesn't look like the real output of bin2hex() though ... Commented Jun 5, 2012 at 11:13
  • 000000001900000068656172746c790000000000000000000000000074757479000000000000000000000000000000003100000001000000190000006172756e006c7900000000000000000000000000747574790000000000000000000000000000000031000000020000001900000073697661006c7900000000000000000000000000747574790000000000000000000000000000000031000000030000001f00000073697661006c7900000000000000000000000000747574790000000000000000000000000000000031000000 This is the content of bin2hex(file_get_contents('table1.txt')); Commented Jun 5, 2012 at 11:27

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

It's not strange, it's called padding. For gcc add ____attribute____ ((____packed____)) to the definition of the struct. Though sizeof(record) returns 48 on 3 different platforms.
@strkol I imagined it had something to do with padding, but it's good to see that confirmed ... but why is it padded to 52 bytes?
I don't know. gcc doesn't insert any padding on this struct. I guess that Smith Dwayne is using some other compiler.
@strkol the weird thing is that the padding doesn't even consist of zeroes; it's (consistently) 0x31000000
then it seems that the bug is in the C program which writes the txt file :-)
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.