473,408 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Why fread mistakes in reading binary file ?

Hello.. i'm using php on linux --version:

PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

and i'm experiencing unexpected behavior using fread with a binary
file.
Look at this code: i have a jpeg image of 2290 bytes long, but fread
cannot read it correctly, like fgetc does:

<code>
$data1 = fread(fopen($f, 'r'),2290);

$h = fopen( $f, 'r' );
while(!feof($h))
$data2 .= fgetc($h);

echo "l1(".strlen($data1).") l2(".strlen($data2).")
size(".filesize($f).")\n";

for($i=0;$i<2290;$i++)
echo "Char($i) = d1(".ord($data1[$i]).") d2(".ord($data2[$i]).")
\n";
</code>

This is the output:

<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
....
</output>

As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
This behaviour is repeated whenever the file has a 0ed byte in its
content

Can anyone of you explain this ?..

Tnx
Jun 2 '08 #1
4 3338
On Thu, 08 May 2008 22:50:16 +0200, Giacomo <gi*************@gmail.com
wrote:
Hello.. i'm using php on linux --version:

PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

and i'm experiencing unexpected behavior using fread with a binary
file.
Look at this code: i have a jpeg image of 2290 bytes long, but fread
cannot read it correctly, like fgetc does:

<code>
$data1 = fread(fopen($f, 'r'),2290);

$h = fopen( $f, 'r' );
while(!feof($h))
$data2 .= fgetc($h);

echo "l1(".strlen($data1).") l2(".strlen($data2).")
size(".filesize($f).")\n";

for($i=0;$i<2290;$i++)
echo "Char($i) = d1(".ord($data1[$i]).") d2(".ord($data2[$i]).")
\n";
</code>

This is the output:

<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
...
</output>

As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
This behaviour is repeated whenever the file has a 0ed byte in its
content
This works here:
<?php
echo phpversion()."\n";
$file = tempnam(dirname(__FILE__),'foo');
$f = fopen($file,'wb');
//Both work:
fwrite($f,chr(224).chr(0).chr(16));
fwrite($f,"a\0b");
fclose($f);
$data1 = fread(fopen($file, 'r'),2290);
$data2= "";
$h = fopen($file,'r');
while(!feof($h))
$data2 .= fgetc($h);
echo "l1(".strlen($data1).") l2(".strlen($data2).")
size(".filesize($file).")\n";
for($i=0;$i<filesize($file);$i++)
echo "Char($i) = d1(".ord($data1[$i]).") d2(".ord($data2[$i]).")\n";
?>
5.2.4
0
l1(6) l2(6) size(6)
Char(0) = d1(224) d2(224)
Char(1) = d1(0) d2(0)
Char(2) = d1(16) d2(16)
Char(3) = d1(97) d2(97)
Char(4) = d1(0) d2(0)
Char(5) = d1(98) d2(98)

What happens if you use fopen($f,'rb');?
--
Rik Wasmus
Jun 2 '08 #2
>
What happens if you use fopen($f,'rb');?
--
Rik Wasmus
No changes, since 'rb' is equal to 'r' in linux; anyway i just migrate
to 5.2.6 and now fread works correctly

Bye bye
Jun 2 '08 #3
Giacomo <gi*************@gmail.comwrote:
>
<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
...
</output>

As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
For the record, did you notice that 92 48 are the ASCII ordinals for "\0"?
Somebody was escaping the nulls in your string.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 2 '08 #4
Greetings, Tim Roberts.
In reply to Your message dated Saturday, May 10, 2008, 08:38:34,
Giacomo <gi*************@gmail.comwrote:
>>
<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
...
</output>

As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
For the record, did you notice that 92 48 are the ASCII ordinals for "\0"?
Somebody was escaping the nulls in your string.
Magic_quotes = On
?
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen...
2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
2
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0,...
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
8
by: vippstar | last post by:
-- foo.c -- #include <stdio.h> int main(void) { struct { int a; int b; } foo; fread(&foo, sizeof foo, 1, stdin); return 0; } -- foo.c --
2
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.