From ripley at stats.ox.ac.uk  Mon Sep  1 23:15:09 2003
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon Sep  1 23:14:35 2003
Subject: [Rd] Re: [R] File Reading Problem (PR#4043)
In-Reply-To: <ggc7lvc0i2od34kdhkc6p0lct3mpu19h78@4ax.com>
Message-ID: <Pine.LNX.4.44.0309012214180.23108-100000@gannet.stats>

On Mon, 1 Sep 2003, Duncan Murdoch wrote:

> On Mon, 1 Sep 2003 22:32:29 +0200 (MET DST), you wrote:
> 
> >On Mon, 1 Sep 2003 14:26:43 -0700, "Jiming Yu"
> ><jimingyu@princeton.edu> wrote:
> >
> >>Dear all,
> >>    I am trying to read characters byte by byte(in their ASCII codes) from a
> >>file
> >
> >I was going to suggest using readBin, but there seems to be a bug:
> >
> >> con <- file('c:/test.txt','rb')
> >> readBin(con,'c',15,1)
> 
> The problem was that the R code assumed 'c' was a storage mode, and
> the C code assumed that it had already been checked as valid.  I'll
> put a patch into r-devel.
> 
> The answer to the original question is to use readChar, not readBin.
> readBin looks for C-style null terminated strings.  readChar can read
> characters one at a time.

It's on the help page for readBin, though.

> 
> The right code to read "This is a book." from file foo.txt is
> 
>  readChar("foo.txt", rep(1, 15))
> 
> Duncan Murdoch
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] Re: [R] File Reading Problem (PR#4043)
Prof Brian Ripley