File Reading Problem (PR#4043)
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