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

Duncan Murdoch dmurdoch at pair.com
Mon Sep 1 18:02:03 MEST 2003


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 at 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.

The right code to read "This is a book." from file foo.txt is

 readChar("foo.txt", rep(1, 15))

Duncan Murdoch



More information about the R-devel mailing list