Skip to content
Back to formatted view

Raw Message

Message-ID: <ggc7lvc0i2od34kdhkc6p0lct3mpu19h78@4ax.com>
Date: 2003-09-01T23:01:20Z
From: Duncan Murdoch
Subject: File Reading Problem (PR#4043)
In-Reply-To: <200309012032.h81KWTJH001670@pubhealth.ku.dk>

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