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
|
#' @title Read line from file, removing newline characters
|
||||||
#' @description Equivalent function to its homonymous Matlab equivalent.
|
#' @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.
|
#' @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
|
#' @author Waldir Leoncio
|
||||||
|
#' @seealso fopen
|
||||||
#' @export
|
#' @export
|
||||||
fgetl <- function(file) {
|
fgetl <- function(file) {
|
||||||
# ==========================================================================
|
# ==========================================================================
|
||||||
# Validation
|
# 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"))) {
|
out <- file[-1]
|
||||||
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")]
|
|
||||||
return(out)
|
return(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,5 +22,6 @@ fgetl <- function(file) {
|
||||||
#' @param filename Path and name of file to be open
|
#' @param filename Path and name of file to be open
|
||||||
#' @return The same as `readLines(filename)`
|
#' @return The same as `readLines(filename)`
|
||||||
#' @author Waldir Leoncio
|
#' @author Waldir Leoncio
|
||||||
|
#' @seealso fgetl
|
||||||
#' @export
|
#' @export
|
||||||
fopen <- function(filename) readLines(filename)
|
fopen <- function(filename) readLines(filename)
|
||||||
23
man/fgetl.Rd
Normal file
23
man/fgetl.Rd
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
23
man/fopen.Rd
Normal file
23
man/fopen.Rd
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue