Re: [Rd] readBin(what="character", n=overcount)->extra "" in result (PR#9363)

From: <bill_at_insightful.com>
Date: Mon 13 Nov 2006 - 20:06:40 GMT


On Mon, 13 Nov 2006 bill@insightful.com wrote:

> When I use readBin() to read an unknown number
> of null-terminated strings from a file by supplying
> an overcount as the n= argument, it appends an empty
> string to the result.
>
> > tf<-tempfile()
> > strings<-c("One","Two","Three")
> > writeBin(strings, con=tf)
> > readBin(con=tf,what="character",n=100) # expect "One","Two","Three" only
> [1] "One" "Two" "Three" ""

I think the fix is to src/library/connections.c, where m (the number of items read) is initialized to 1 instead of 0. It is later used to shorten the vector of read objects:

    if(m < n) {

        PROTECT(ans = lengthgets(ans, m));
        UNPROTECT(1);

    }
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Nov 14 07:14:50 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 13 Nov 2006 - 21:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.