Skip to content
Prev 355509 / 398500 Next

Can scan() detect end-of-file?

I don't know what OS-independent function you use in C that performs the
way you describe. I would write the below function in C myself in order to get 
this functionality in that language.

readListOfVectors <- function( input ) {
  lines <- readLines( input )
  if ( "" == lines[ length( lines ) ] ) {
   lines <- lines[ -length( lines ) ]
  }
  result <- lapply( lines
                  , function( lin ) {
                     lc <- textConnection( lin )
                     res <- scan( lc, quiet=TRUE )
                     close( lc )
  		    res
 		   }
                  )
  result
}

# test
txt <- (
"1 2 3 4
1 4 5
2 4 6 8 9
")

tc <- textConnection(txt)
# can give it a filename or a connection object
readListOfVectors( tc )
close(tc)
On Thu, 15 Oct 2015, William Dunlap wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k