title |
---|
IO |
[TOC]
Experimental
Loads a rank-2 array
from a text file.
call [[stdlib_io(module):loadtxt(interface)]](filename, array)
filename
: Shall be a character expression containing the file name from which to load the rank-2 array
.
array
: Shall be an allocatable rank-2 array of type real
, complex
or integer
.
Returns an allocated rank-2 array
with the content of filename
.
program demo_loadtxt
use stdlib_io, only: loadtxt
implicit none
real, allocatable :: x(:,:)
call loadtxt('example.dat', x)
end program demo_loadtxt
Experimental
Returns the unit number of a file opened to read, to write, or to read and write. The file might be a text file or a binary file. All files are opened using a streamed access.
u = [[stdlib_io(module):open(function)]](filename [, mode] [, iostat])
filename
: Shall be a character expression containing the name of the file to open.
mode
(optional): Shall be a character expression containing characters describing the way in which the file will be used. The available modes are:
Character | Meaning |
---|---|
'r' |
open for reading (default) |
'w' |
open for writing, truncating the file first |
'x' |
open for exclusive creation, failing if the file already exists |
'a' |
open for writing, appending to the end of the file if it exists |
'+' |
open for updating (reading and writing) |
'b' |
binary mode |
't' |
text mode (default) |
The default mode
is 'rt'
(i.e. open for reading a text file). The mode
may include one of the four different methods for opening a file (i.e., 'r'
, 'w'
, 'x'
, and 'a'
). These four methods can be associated with the character '+'
to open the file for updating. In addition, it can be specified if the file should be handled as a binary file ('b'
) or a text file ('t'
).
iostat
(optional): Shall be a scalar of type integer
that receives the error status of open
, if provided. If no error exists, iostat
is zero.
u
: Shall be a scalar of type integer
that specifies the unit number associated with the file filename
.
The result is a scalar of type integer
.
program demo_open
use stdlib_io, only: open
implicit none
integer :: u
u = open('example.dat', 'wt')
write(u,'(a)')'This is an example for open'
close(u)
end program demo_open
Experimental
Saves a rank-2 array
into a text file.
call [[stdlib_io(module):savetxt(interface)]](filename, array)
filename
: Shall be a character expression containing the name of the file that will contain the 2D array
.
array
: Shall be a rank-2 array of type real
, complex
or integer
.
Provides a text file called filename
that contains the rank-2 array
.
program demo_savetxt
use stdlib_io, only: savetxt
implicit none
real :: x(3,2) = 1
call savetxt('example.dat', x)
end program demo_savetxt
Experimental
Impure subroutine.
Outputs a logical/integer/real/complex/character/string_type
scalar,
or logical/integer/real/complex/string_type
and rank-1/rank-2 array to the screen or a file unit
.
call [[stdlib_io(module):disp(interface)]]( [x, header, unit, brief, format, width, sep] )
-
x
: Shall be alogical/integer/real/complex/character(len=*)/string_type
scalar orlogical/integer/real/complex/string_type
and rank-1/rank-2 array. This argument isintent(in)
andoptional
. -
header
: Shall be acharacter(len=*)
scalar. This argument isintent(in)
andoptional
. -
unit
: Shall be aninteger
scalar, linked to an IO stream. This argument isintent(in)
andoptional
.
The default value isoutput_unit
fromiso_fortran_env
module. -
brief
: Shall be alogical
scalar, controls an abridged version of thex
array to be outputted. This argument isintent(in)
andoptional
.
The default value is.false.
-
format
: Shall be acharacter(len=*)
scalar. This argument isintent(in)
andoptional
.
The default value isg0.4
. -
width
: Shall be aninteger
scalar, controls the outputted maximum width (>=80
). This argument isintent(in)
andoptional
.
The default value is80
. -
sep
: Shall be acharacter(len=*)
scalar, separator. This argument isintent(in)
andoptional
.
The default value is " ", two spaces.
The result is to print header
and x
on the screen (or another output unit/file
) in this order.
If disp
is not passed any arguments, a blank line will be printed.
program test_io_disp
use stdlib_io, only: disp
real :: r(2, 3)
complex :: c(2, 3), c_3d(2, 100, 20)
integer :: i(2, 3)
logical :: l(10, 10)
r = 1.; c = 1.; c_3d = 2.; i = 1; l = .true.
c_3d(1,3,1) = (1000, 0.001)
call disp('string', header='disp(string):')
call disp('It is a note.')
call disp()
call disp(r, header='disp(r):')
call disp(r(1,:), header='disp(r(1,:))', format="f6.2")
call disp(c, header='disp(c):')
call disp(i, header='disp(i):', sep=",")
call disp(l, header='disp(l):', brief=.true.)
call disp(c_3d(:,3,1:10), header='disp(c_3d(:,3,1:10)):', width=100)
call disp(c_3d(2,:,:), header='disp(c_3d(2,:,:)):', brief=.true.)
end program test_io_disp
Results:
disp(string):
string
It is a note.
disp(r):
[matrix size: 2×3]
1.000 1.000 1.000
1.000 1.000 1.000
disp(r(1,:))
[vector size: 3]
1.00 1.00 1.00
disp(c):
[matrix size: 2×3]
(1.000,0.000) (1.000,0.000) (1.000,0.000)
(1.000,0.000) (1.000,0.000) (1.000,0.000)
disp(i):
[matrix size: 2×3]
1, 1, 1,
1, 1, 1,
disp(l):
[matrix size: 10×10]
T T T .. T
T T T .. T
T T T .. T
: : : : :
T T T .. T
disp(c_3d(:,3,1:10)):
[matrix size: 2×10]
(1000.,0.1000E-2) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) &
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000)
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) &
(2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000) (2.000,0.000)
disp(c_3d(2,:,:)):
[matrix size: 100×20]
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000)
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000)
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000)
: : : : :
(2.000,0.000) (2.000,0.000) (2.000,0.000) .. (2.000,0.000)