Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Truncate-a-file/R/truncate-a-file.r
Normal file
28
Task/Truncate-a-file/R/truncate-a-file.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
truncate_file <- function(filename, n_bytes) {
|
||||
|
||||
# check file exists and size is greater than n_bytes
|
||||
stopifnot(
|
||||
"file does not exist"= file.exists(filename),
|
||||
"not enough bytes in file"= file.size(filename) >= n_bytes
|
||||
)
|
||||
|
||||
# read bytes from input file
|
||||
input.con <- file(filename, "rb")
|
||||
bindata <- readBin(input.con, integer(), n=n_bytes/4)
|
||||
close(input.con)
|
||||
|
||||
# write bytes to temporary file
|
||||
tmp.filename <- tempfile()
|
||||
output.con <- file(tmp.filename, "wb")
|
||||
writeBin(bindata, output.con)
|
||||
close(output.con)
|
||||
|
||||
# double check that everything worked before overwriting original file
|
||||
stopifnot(
|
||||
"temp file is not expected size"= file.size(tmp.filename) == n_bytes
|
||||
)
|
||||
|
||||
# overwrite input file with temporary file
|
||||
file.rename(tmp.filename, filename)
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue