stdlib_io#
See also the specification page: stdlib_io.
stdlib_io (module)#
Provides a support for file handling (Specification: stdlib_io)
version: experimental
Reads a whole ASCII file and loads its contents into a string variable. (Specification: stdlib_io)
Summary
Subroutine interface for reading the content of a file into a string.
Description
This subroutine reads the entirety of a specified ASCII file and returns it as a string. The optional err argument allows for handling errors through the library’s state_type class. An optional logical flag can be passed to delete the file after reading.
Notes
Handles errors using the library’s state_type error-handling class. If not provided, exceptions will trigger an error stop.
Interfaces
get_file (interface)#
-
get_file (interface) - Procedures:
get_line (interface)#
-
get_line (interface) Read a whole line from a formatted unit into a string variable
loadtxt (interface)#
-
loadtxt (interface) Loads a 2D array from a text file (Specification: stdlib_io)
savetxt (interface)#
-
savetxt (interface) Saves a 2D array into a text file (Specification: stdlib_io)
Procedures
open (function)#
-
function open(filename, mode, iostat) -> u Opens a file (Specification: stdlib_io)
- Arguments:
- filenamecharacter(len=*), intent(in)
- modecharacter(len=*), intent(in), optional
- iostatinteger, intent(out), optional
- Returns:
- uinteger
Behavior
To open a file to read:
u = open("somefile.txt") ! The default `mode` is "rt" u = open("somefile.txt", "r")
To open a file to write:
u = open("somefile.txt", "w")
To append to the end of the file if it exists:
u = open("somefile.txt", "a")
parse_mode (function)#
-
function parse_mode(mode) -> mode_ - Arguments:
- modecharacter(len=*), intent(in)
- Returns:
- mode_character(len=3)
loadtxt_rsp (subroutine)#
-
subroutine loadtxt_rsp(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dreal(kind=sp), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_rdp (subroutine)#
-
subroutine loadtxt_rdp(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dreal(kind=dp), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_iint8 (subroutine)#
-
subroutine loadtxt_iint8(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dinteger(kind=int8), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_iint16 (subroutine)#
-
subroutine loadtxt_iint16(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dinteger(kind=int16), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_iint32 (subroutine)#
-
subroutine loadtxt_iint32(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dinteger(kind=int32), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_iint64 (subroutine)#
-
subroutine loadtxt_iint64(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dinteger(kind=int64), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_csp (subroutine)#
-
subroutine loadtxt_csp(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dcomplex(kind=sp), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
loadtxt_cdp (subroutine)#
-
subroutine loadtxt_cdp(filename, d, skiprows, max_rows, fmt, delimiter) Loads a 2D array from a text file.
Arguments#
Filename to load the array from
- Arguments:
- filenamecharacter(len=*), intent(in)
The array ‘d’ will be automatically allocated with the correct dimensions
- dcomplex(kind=dp), allocatable, (:, :), intent(out), dimension(:,:)
Skip the first skiprows lines. If skipping more rows than present, a 0-sized array will be returned. The default is 0.
- skiprowsinteger, intent(in), optional
Read max_rows lines of content after skiprows lines. A negative value results in reading all lines. A value of zero results in no lines to be read. The default value is -1.
- max_rowsinteger, intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- delimitercharacter(len=1), intent(in), optional
savetxt_rspf (subroutine)#
-
subroutine savetxt_rspf(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dreal(kind=sp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
real(sp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_rdpf (subroutine)#
-
subroutine savetxt_rdpf(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dreal(kind=dp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
real(dp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint8f (subroutine)#
-
subroutine savetxt_iint8f(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dinteger(kind=int8), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int8) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint16f (subroutine)#
-
subroutine savetxt_iint16f(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dinteger(kind=int16), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int16) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint32f (subroutine)#
-
subroutine savetxt_iint32f(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dinteger(kind=int32), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int32) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint64f (subroutine)#
-
subroutine savetxt_iint64f(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dinteger(kind=int64), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int64) :: data(3, 2) call savetxt("log.txt", data)
savetxt_cspf (subroutine)#
-
subroutine savetxt_cspf(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dcomplex(kind=sp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
complex(sp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_cdpf (subroutine)#
-
subroutine savetxt_cdpf(filename, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- filenamecharacter(len=*), intent(in)
- dcomplex(kind=dp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
complex(dp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_rspu (subroutine)#
-
subroutine savetxt_rspu(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dreal(kind=sp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
real(sp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_rdpu (subroutine)#
-
subroutine savetxt_rdpu(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dreal(kind=dp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
real(dp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint8u (subroutine)#
-
subroutine savetxt_iint8u(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dinteger(kind=int8), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int8) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint16u (subroutine)#
-
subroutine savetxt_iint16u(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dinteger(kind=int16), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int16) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint32u (subroutine)#
-
subroutine savetxt_iint32u(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dinteger(kind=int32), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int32) :: data(3, 2) call savetxt("log.txt", data)
savetxt_iint64u (subroutine)#
-
subroutine savetxt_iint64u(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dinteger(kind=int64), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
integer(int64) :: data(3, 2) call savetxt("log.txt", data)
savetxt_cspu (subroutine)#
-
subroutine savetxt_cspu(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dcomplex(kind=sp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
complex(sp) :: data(3, 2) call savetxt("log.txt", data)
savetxt_cdpu (subroutine)#
-
subroutine savetxt_cdpu(unit, d, delimiter, fmt, header, footer, comments) Saves a 2D array into a text file.
Arguments#
- Arguments:
- unitinteger, intent(in)
- dcomplex(kind=dp), (:, :), intent(in), dimension(:,:)
- delimitercharacter(len=*), intent(in), optional
- fmtcharacter(len=*), intent(in), optional
- headercharacter(len=*), intent(in), optional
- footercharacter(len=*), intent(in), optional
- commentscharacter(len=*), intent(in), optional
Example#
complex(dp) :: data(3, 2) call savetxt("log.txt", data)
get_line_char (subroutine)#
-
subroutine get_line_char(unit, line, iostat, iomsg) Read a whole line from a formatted unit into a deferred length character variable
- Arguments:
- unitinteger, intent(in)
Formatted IO unit
- linecharacter(len=:), allocatable, intent(out)
Line to read
- iostatinteger, intent(out), optional
Status of operation
- iomsgcharacter(len=:), allocatable, optional
Error message
get_line_string (subroutine)#
-
subroutine get_line_string(unit, line, iostat, iomsg) Read a whole line from a formatted unit into a string variable
- Arguments:
- unitinteger, intent(in)
Formatted IO unit
- linetype(string_type), intent(out)
Line to read
- iostatinteger, intent(out), optional
Status of operation
- iomsgcharacter(len=:), allocatable, optional
Error message
get_line_input_char (subroutine)#
-
subroutine get_line_input_char(line, iostat, iomsg) Read a whole line from the standard input into a deferred length character variable
- Arguments:
- linecharacter(len=:), allocatable, intent(out)
Line to read
- iostatinteger, intent(out), optional
Status of operation
- iomsgcharacter(len=:), allocatable, optional
Error message
get_line_input_string (subroutine)#
-
subroutine get_line_input_string(line, iostat, iomsg) Read a whole line from the standard input into a string variable
- Arguments:
- linetype(string_type), intent(out)
Line to read
- iostatinteger, intent(out), optional
Status of operation
- iomsgcharacter(len=:), allocatable, optional
Error message
get_file_string (subroutine)#
-
subroutine get_file_string(filename, file, err, delete) Reads a whole ASCII file and loads its contents into a string variable. The function handles error states and optionally deletes the file after reading.
- Arguments:
- filenamecharacter(len=*), intent(in)
Input file name
- filetype(string_type), intent(out)
Output string variable
- errtype(state_type), intent(out), optional
[optional] State return flag. On error, if not requested, the code will stop.
- deletelogical, intent(in), optional
[optional] Delete file after reading? Default: do not delete
get_file_char (subroutine)#
-
subroutine get_file_char(filename, file, err, delete) Reads a whole ASCII file and loads its contents into an allocatable character variable. The function handles error states and optionally deletes the file after reading. Check if the file should be deleted after reading Check file existing Retrieve file size
- Arguments:
- filenamecharacter(len=*), intent(in)
Input file name
- filecharacter(len=:), allocatable, intent(out)
Output string variable
- errtype(state_type), intent(out), optional
[optional] State return flag. On error, if not requested, the code will stop.
- deletelogical, intent(in), optional
[optional] Delete file after reading? Default: do not delete