Skip to contents

str_write_lines() writes each element of string followed by sep, including the last one, and converts the output to encoding. It is the counterpart of str_read_lines().

Usage

str_write_lines(string, con, encoding = "UTF-8", sep = "\n")

Arguments

string

A character vector to write.

con

A file name or a connection opened in binary mode.

encoding

A single string giving the output encoding. NULL or "" uses the current default encoding.

sep

A single string written after each element. Defaults to "\n" on all platforms.

Value

string, invisibly and unchanged.

Details

Missing values are an error. Strings marked with the "bytes" encoding are an error, as in str_c(). Characters that the output encoding cannot represent are written as that encoding's substitution character, with a warning.

See also

str_read_lines(); stringi::stri_write_lines(), which writes an empty file instead of signalling an error for missing values.

Examples

path <- tempfile()
str_write_lines(c("first", "second"), path)
str_read_lines(path, encoding = "UTF-8")
#> [1] "first"  "second"
unlink(path)