Reimplemented fgetl
This commit is contained in:
parent
f036a33bca
commit
372500d750
3 changed files with 52 additions and 12 deletions
18
R/fgetl.R
18
R/fgetl.R
|
|
@ -1,26 +1,19 @@
|
|||
#' @title Read line from file, removing newline characters
|
||||
#' @description Equivalent function to its homonymous Matlab equivalent.
|
||||
#' @param file file to be read
|
||||
#' @param file character vector to be read, usually an output of `fopen()`
|
||||
#' @return If the file is nonempty, then fgetl returns tline as a character vector. If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.
|
||||
#' @author Waldir Leoncio
|
||||
#' @seealso fopen
|
||||
#' @export
|
||||
fgetl <- function(file) {
|
||||
# ==========================================================================
|
||||
# Validation
|
||||
# ==========================================================================
|
||||
if (file == "") return(-1)
|
||||
if (length(file) <= 1) return(-1)
|
||||
# ==========================================================================
|
||||
# Determine next line to be read
|
||||
# Returning file minus the first line
|
||||
# ==========================================================================
|
||||
if (is.null(attr(file, "last_read_line"))) {
|
||||
attr(file, "last_read_line") <- 1
|
||||
} else {
|
||||
attr(file, "last_read_line") <- attr(file, "last_read_line") + 1
|
||||
}
|
||||
# ==========================================================================
|
||||
# Returning next line
|
||||
# ==========================================================================
|
||||
out <- file[attr(file, "last_read_line")]
|
||||
out <- file[-1]
|
||||
return(out)
|
||||
}
|
||||
|
||||
|
|
@ -29,5 +22,6 @@ fgetl <- function(file) {
|
|||
#' @param filename Path and name of file to be open
|
||||
#' @return The same as `readLines(filename)`
|
||||
#' @author Waldir Leoncio
|
||||
#' @seealso fgetl
|
||||
#' @export
|
||||
fopen <- function(filename) readLines(filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue