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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon 13 Nov 2006 - 21:18:15 GMT

That's arrived after I had committed an identical fix. It's a relic of a time when m had a different meaning.

Thanks!

Brian

On Mon, 13 Nov 2006, Bill Dunlap wrote:

> 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);
> }
>
> *** connections.c-orig 2006-09-13 19:05:06.000000000 -0700
> --- connections.c 2006-11-13 11:46:35.000000000 -0800
> ***************
> *** 2740,2746 ****
> if(!strcmp(what, "character")) {
> SEXP onechar;
> PROTECT(ans = allocVector(STRSXP, n));
> ! for(i = 0, m = i+1; i < n; i++) {
> onechar = isRaw ? rawOneString(bytes, nbytes, &np)
> : readOneString(con);
> if(onechar != R_NilValue) {
> --- 2740,2746 ----
> if(!strcmp(what, "character")) {
> SEXP onechar;
> PROTECT(ans = allocVector(STRSXP, n));
> ! for(i = 0, m = 0; i < n; i++) {
> onechar = isRaw ? rawOneString(bytes, nbytes, &np)
> : readOneString(con);
> if(onechar != R_NilValue) {
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue Nov 14 08:22:23 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.