[R] Quickest way to make a large "empty" file on disk?

Henrik Bengtsson hb at biostat.ucsf.edu
Thu May 3 00:45:43 CEST 2012


An R solution is:

allocateFile <- function(pathname, nbrOfBytes) {
  con <- file(pathname, open="wb");
  on.exit(close(con));
  seek(con, where=nbrOfBytes-1L, origin="start", rw="write");
  writeBin(as.raw(0), con=con);
  invisible(pathname);
} # allocateFile()

> allocateFile("foo.bin", nbrOfBytes=985403)
> file.info("foo.bin")$size
[1] 985403

Note sure if it works on all OSes/file systems.

/Henrik

On Wed, May 2, 2012 at 3:23 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:
> R-helpers:
>
> What would be the absolute fastest way to make a large "empty" file (e.g.
> filled with all zeroes) on disk, given a byte size and a given number
> number of empty values.  I know I can use writeBin, but the "object" in
> this case may be far too large to store in main memory.  I'm asking because
> I'm going to use this file in conjunction with mmap to do parallel writes
> to this file.  Say, I want to create a blank file of 10,000 floating point
> numbers.
>
> Thanks!
>
> --j
>
> --
> Jonathan A. Greenberg, PhD
> Assistant Professor
> Department of Geography and Geographic Information Science
> University of Illinois at Urbana-Champaign
> 607 South Mathews Avenue, MC 150
> Urbana, IL 61801
> Phone: 415-763-5476
> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> http://www.geog.illinois.edu/people/JonathanGreenberg.html
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list