zzZzz has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, I am reading different numbers of bytes from a binary file using read.
At one point I am reading a byte at a time.
Some bytes are read correctly but then a call to

read($filehandle, $var, 1)

sets $var to the next byte than the one that is supposed to return.
Also tell says that I moved two bytes from the previous position
in the file.

How can this be explained?

I am using perl v5.8.4 in XP.

Thanks in advance,

Z

Replies are listed 'Best First'.
Re: Problem with read()
by fglock (Vicar) on Dec 09, 2004 at 13:17 UTC

    Maybe the file has UTF8 characters on it? From the "read" docs (perldoc -f read):

    Note the characters: depending on the status of the filehandle, either (8-bit) bytes or characters are read. By default all filehandles operate on bytes, but for example if the filehandle has been opened with the ":utf8" I/O layer (see "open", and the "open" pragma, open), the I/O will operate on UTF-8 encoded Unicode characters, not bytes. Similarly for the ":encoding" pragma: in that case pretty much any characters can be read.

Re: Problem with read()
by Anonymous Monk on Dec 14, 2004 at 10:16 UTC
    Again I was the unfortunate to come across a flaw in an internal function….
    There wasn’t any chance that my code was wrong. I didn’t forget to
    apply binmode to the filehandle, did I?
    :)