Skip to content

readBin(what="character", n=overcount)->extra "" in result (PR#9361)

3 messages · Bill Dunlap, Brian Ripley

#
Full_Name: Bill Dunlap
Version: 2.4.0
OS: Windows XP
Submission from: (NULL) (208.252.71.182)


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.
[1] "One"   "Two"   "Three" ""
[1]  79 110 101   0  84 119 111   0  84 104 114 101 101   0
_                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          4.0                         
year           2006                        
month          10                          
day            03                          
svn rev        39566                       
language       R                           
version.string R version 2.4.0 (2006-10-03)
#
On Mon, 13 Nov 2006 bill at insightful.com wrote:

            
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) {
#
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: