From ripley@stats.ox.ac.uk  Fri Nov 29 15:57:03 2002
From: ripley@stats.ox.ac.uk (ripley@stats.ox.ac.uk)
Date: Fri Nov 29 15:57:03 2002
Subject: [Rd] readBin or writeBin adds extra nulls (PR#2333)
In-Reply-To: <200211290345.EAA05952@pubhealth.ku.dk>
Message-ID: <Pine.LNX.4.31.0211291450370.16177-100000@gannet.stats>

Please read the help page:

     `readBin' and `writeBin' read and write C-style zero-terminated
     character strings.  `readChar' and `writeChar' allow more
     flexibility, and can also be used on text-mode connections.

You don't have such character strings in a postscript file (if that is
what foo.ps is).  The code arbitrarily breaks the input into strings of
length 10000 if it gets in trouble finding a null terminator.  The bug is
not issuing a warning to the user if that happens.

You don't get the last part because there was no final null terminator,
so no qualifying string gets read.

You should be using either readChar (as the help page says) if you have a
text file, or integer() of size 1 if you really want to read a file of
bytes.

On Fri, 29 Nov 2002 ken_yap_aus@yahoo.com wrote:

> Full_Name: Ken Yap
> Version: 1.6.1
> OS: Linux (SuSE 8.0)
> Submission from: (NULL) (129.78.64.5)
>
>
> I'm trying to copy a file using readBin and writeBin. (The reason is to be able
> to pipe PostScript or PDF output to a socket later, this is just an experiment.)
> I do:
>
> zz <- file("foo.ps", "rb")
> r <- readBin(zz, character(), 1000000)
> yy <- file("bar.ps", "wb")
> writeBin(r, yy, character(), 1000000)
> close(yy)
> close(zz)
>
> When I diff the files I see differences. Every 10000 characters, the output file
> has
> an extra null byte, and the last portion is missing. Maybe the missing part is
> because I forgot to flush the output of something, if someone could explain how
> to do it correctly that would be nice. The extra null byte seems to be due to an
> off-by-one programming error somewhere perhaps? Or am I doing something wrong?
>
> Thanks for the great software by the way.
>
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] readBin or writeBin adds extra nulls (PR#2333)
ripley@stats.ox.ac.uk