From dmurdoch at pair.com  Mon Sep  1 18:02:03 2003
From: dmurdoch at pair.com (Duncan Murdoch)
Date: Mon Sep  1 23:01:20 2003
Subject: [Rd] Re: [R] File Reading Problem (PR#4043)
In-Reply-To: <200309012032.h81KWTJH001670@pubhealth.ku.dk>
References: <200309012032.h81KWTJH001670@pubhealth.ku.dk>
Message-ID: <ggc7lvc0i2od34kdhkc6p0lct3mpu19h78@4ax.com>

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.

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

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

Duncan Murdoch


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