ID EN
I/O & Files

readBin

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Membaca data biner dari atau menulis data biner ke koneksi atau vektor mentah.

Syntax

R
readBin(con, what, n = 1L, size = NA_integer_, signed = TRUE,
        endian = .Platform$endian)<p></p><p>writeBin(object, con, size = NA_integer_,
         endian = .Platform$endian, useBytes = FALSE)</p>

Arguments

Parameter Deskripsi
con A connection object or a character string naming a file or a raw vector.
what Either an object whose mode will give the mode of the vector to be read, or a character vector of length one describing the mode: one of "numeric", "double", "integer", "int", "logical", "complex", "character", "raw".
n numeric. The (maximal) number of records to be read. You can use an over-estimate here, but not too large as storage is reserved for n items.
size integer. The number of bytes per element in the byte stream. The default, NA_integer_, uses the natural size. Size changing is not supported for raw and complex vectors.
signed logical. Only used for integers of sizes 1 and 2, when it determines if the quantity on file should be regarded as a signed or unsigned integer.
endian The endian-ness ("big" or "little") of the target system for the file. Using "swap" will force swapping endian-ness.
object An R object to be written to the connection.
useBytes See writeLines.

Return Value

Untuk readBin, vektor mode yang sesuai dan panjang jumlah item yang dibaca (yang mungkin kurang dari n). Untuk writeBin, vektor mentah (jika con adalah vektor mentah) atau NULL yang tidak terlihat.

Details

Fungsi-fungsi ini hanya dapat digunakan dengan koneksi mode biner. Jika con adalah string karakter, fungsi memanggil file untuk mendapatkan koneksi file mode biner yang dibuka selama pemanggilan fungsi. Jika koneksi terbuka maka dibaca/ditulis dari posisinya saat ini. Jika tidak terbuka, maka dibuka selama panggilan dalam mode yang sesuai (baca atau tulis biner) dan kemudian ditutup kembali. Koneksi terbuka harus dalam mode biner. Jika readBin dipanggil dengan vektor mentah, data dalam vektor tersebut digunakan sebagai masukan. Jika writeBin dipanggil dengan vektor mentah, itu hanyalah indikasi bahwa vektor mentah harus dikembalikan. Jika ukuran ditentukan dan bukan ukuran alami objek, setiap elemen vektor dipaksa ke tipe yang sesuai sebelum ditulis atau sebagai i

Contoh

Example
R
# NOT RUN {
zzfil <- tempfile("testbin")
zz <- file(zzfil, "wb")
writeBin(1:10, zz)
writeBin(pi, zz, endian = "swap")
writeBin(pi, zz, size = 4)
writeBin(pi^2, zz, size = 4, endian = "swap")
writeBin(pi+3i, zz)
writeBin("A test of a connection", zz)
z <- paste("A very long string", 1:100, collapse = " + ")
writeBin(z, zz)
if(.Machine$sizeof.long == 8 || .Machine$sizeof.longlong == 8)
    writeBin(as.integer(5^(1:10)), zz, size = 8)
if((s <- .Machine$sizeof.longdouble) > 8)
    writeBin((pi/3)^(1:10), zz, size = s)
close(zz)

zz <- file(zzfil, "rb")
readBin(zz, integer(), 4)
readBin(zz, integer(), 6)
readBin(zz, numeric(), 1, endian = "swap")
readBin(zz, numeric(), size = 4)
readBin(zz, numeric(), size = 4, endian = "swap")
readBin(zz, complex(), 1)
readBin(zz, character(), 1)
z2 <- readBin(zz, character(), 1)
if(.Machine$sizeof.long == 8 || .Machine$sizeof.longlong == 8)
    readBin(zz, integer(), 10,  size = 8)
if((s <- .Machine$sizeof.longdouble) > 8)
    readBin(zz, numeric(), 10, size = s)
close(zz)
unlink(zzfil)
stopifnot(z2 == z)

## signed vs unsigned ints
zzfil <- tempfile("testbin")
zz <- file(zzfil, "wb")
x <- as.integer(seq(0, 255, 32))
writeBin(x, zz, size = 1)
writeBin(x, zz, size = 1)
x <- as.integer(seq(0, 60000, 10000))
writeBin(x, zz, size = 2)
writeBin(x, zz, size = 2)
close(zz)
zz <- file(zzfil, "rb")
readBin(zz, integer(), 8, size = 1)
readBin(zz, integer(), 8, size = 1, signed = FALSE)
readBin(zz, integer(), 7, size = 2)
readBin(zz, integer(), 7, size = 2, signed = FALSE)
close(zz)
unlink(zzfil)

## use of raw
z <- writeBin(pi^{1:5}, raw(), size = 4)
readBin(z, numeric(), 5, size = 4)
z <- writeBin(c("a", "test", "of", "character"), raw())
readBin(z, character(), 4)
# }

See Also

The ‘R Data Import/Export’ manual. readChar to read/write fixed-length strings. connections readLines writeLines. .Machine for the sizes of long long long and long double.