[R] End of File for binary files

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jan 24 12:56:17 CET 2010


On 23/01/2010 10:40 PM, rn00b wrote:
> I am using readBin to continuously read characters from the binary file. I'm
> trying to figure out how many characters are in the file. What I would like
> to do is something like
> (while! EOF)
> {
> charRead <-.Internal(readBin(con,"character",1L,NA,TRUE,swap))
> i++
> }
> 
> I'm not clear on how to determine the EOF condition in this case. 

You should not be calling .Internal.  It's for internal use, subject to 
change, etc.

Using readBin(...)  you can detect EOF by reading fewer than n items 
when you ask for n.  So the loop would be something like

while (length(charRead <- readBin(con, "character")) > 0) {
   i <- i + 1
}

Duncan Murdoch



More information about the R-help mailing list