diff --git a/R/fgetl.R b/R/fgetl.R index 5e4eec6..176a330 100644 --- a/R/fgetl.R +++ b/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) \ No newline at end of file diff --git a/man/fgetl.Rd b/man/fgetl.Rd new file mode 100644 index 0000000..cd49776 --- /dev/null +++ b/man/fgetl.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fgetl.R +\name{fgetl} +\alias{fgetl} +\title{Read line from file, removing newline characters} +\usage{ +fgetl(file) +} +\arguments{ +\item{file}{character vector to be read, usually an output of `fopen()`} +} +\value{ +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. +} +\description{ +Equivalent function to its homonymous Matlab equivalent. +} +\seealso{ +fopen +} +\author{ +Waldir Leoncio +} diff --git a/man/fopen.Rd b/man/fopen.Rd new file mode 100644 index 0000000..3e2d3a7 --- /dev/null +++ b/man/fopen.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fgetl.R +\name{fopen} +\alias{fopen} +\title{Open file} +\usage{ +fopen(filename) +} +\arguments{ +\item{filename}{Path and name of file to be open} +} +\value{ +The same as `readLines(filename)` +} +\description{ +Open a text file +} +\seealso{ +fgetl +} +\author{ +Waldir Leoncio +}