Useful tools to manipulate files in Fortran programs.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character, | public, | parameter | :: | filesep | = | '/' |
Path separator: '\' for Windows and '/' for Linux, MacOS and other OS. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed. The return variable must have ALLOCATABLE attribute, and must not be allocated (the subroutine takes care of allocation but not about deallocation).
call readMatrix(filename, matrix)
Where:
filename
: string of any length with the path to the file to read. If the file does not exist or
it is empty a Fortran runtime error is raised. If the file does not have the proper format
the behaviour is undefined.matrix
: an allocatable array of rank 2 of any of the integer or real kinds supported.
It will be allocated automatically in the subroutine, but the user must deallocate matrix
manually after use.The following example program loads values from a file named matrix.txt:
PROGRAM readMatrixExample
USE FU_Files, ONLY: readMatrix
USE FU_Prec, ONLY: dp
IMPLICIT NONE
CHARACTER(LEN=:), ALLOCATABLE :: matrixFileName
REAL(KIND=dp), DIMENSION(:,:), ALLOCATABLE :: matrix
matrixFileName = 'matrix.txt'
CALL readMatrix(matrixFileName, matrix)
WRITE(*,*) matrix
DEALLOCATE(matrix)
END PROGRAM readMatrixExample
File matrix.txt contains the following information:
2 3 F
1.1 1.2 1.3
2.1 2.2 2.3
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
integer(kind=i8), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
integer(kind=i16), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
integer(kind=i32), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
integer(kind=i64), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
real(kind=sp), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
real(kind=dp), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Reads a matrix from a file. The file must have the proper format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns must be separated using blanks.
The file is then opened, read and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to read the matrix from. |
||
real(kind=qp), | intent(out), | DIMENSION(:,:), ALLOCATABLE | :: | res |
Values read from the matrix. This output variable must be allocatable but must not be allocated prior to call readMatrix as it is allocated here. However deallocation must be done manually by the user when finishes using the information. |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
call writeMatrix(filename, matrix, header, formato)
Where:
filename
: string of any length with the path to the file to write.matrix
: an array of rank 2 of any of the integer or real kinds supported.header
: optional character variable of any length to write in the second line
of the file.formato
: optional character variable with the format to use for the numbers (without
parenthesis.The following example program writes values to a file named matrix.txt:
PROGRAM writeMatrixExample
USE FU_Files, ONLY: writeMatrix
USE FU_Prec, ONLY: dp
IMPLICIT NONE
CHARACTER(LEN=:), ALLOCATABLE :: matrixFileName
REAL(KIND=dp), DIMENSION(2,3) :: matrix
matrixFileName = 'matrix.txt'
matrix(1,1) = 1.1
matrix(1,2) = 1.2
matrix(1,3) = 1.3
matrix(2,1) = 2.1
matrix(2,2) = 2.2
matrix(2,3) = 2.3
CALL writeMatrix(matrixFileName, matrix, formato='F3.1')
END PROGRAM writeMatrixExample
After execution file matrix.txt contains the following information:
2 3 F
1.1 1.2 1.3
2.1 2.2 2.3
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
integer(kind=i8), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: I3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
integer(kind=i16), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: I3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
integer(kind=i32), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: I3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
integer(kind=i64), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: I3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
real(kind=sp), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: F8.3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
real(kind=dp), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: F8.3 |
Writes a matrix to a file. The file will have the following format:
First line includes the number of rows, the number of columns and a logical indicating if the second line is a header line to be skipped. Example: 5 9 T.
The second row can be a header or not.
Then the matrix comes. The different columns are separated using blanks.
The file is then opened, written and closed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Filename of the file to write the matrix to. |
||
real(kind=qp), | intent(in), | DIMENSION(:,:) | :: | matrix |
Values of the matrix to write to the file. |
|
character(len=*), | intent(in), | optional | :: | header |
Header to be writen in the second line. |
|
character(len=*), | intent(in), | optional | :: | formato |
Format to use for the numbers without parenthesis. Example: F8.3 |
Creates a directory.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | dir |
Path and name of the directory to be created. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if the process has been succesful. False in case of error.
Copies a file or directory.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | src |
Name of the file to be copied. |
||
character(len=*), | intent(in) | :: | dest |
Name of the destination file. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if the process has been succesful. False in case of error.
Moves or renames a file or directory.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | src |
Name of the file to be moved. |
||
character(len=*), | intent(in) | :: | dest |
Name of the destination file. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if the process has been succesful. False in case of error.
Removes a file or directory.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Name of the file or directory to be removed. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if the process has been succesful. False in case of error.
Checks if a file or directory exists.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Name of the file to be check for existence. |
True if the file exists. False otherwise.
Checks if a directory exists.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Name of the directory to be checked. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if fname is a directory. False otherwise.
Checks if a file is empty.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Name of the file to be checked. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if fname is an empty file. False otherwise.
Checks if a regular file exists and it is not a directory, etc.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Name of the file to be checked. |
||
logical, | intent(in), | optional | :: | ignoreErrors |
False to print a detailed description of the error message. Optional parameter. Default is False. |
True if fname is a regular file. False otherwise.
Determines if a path is absolute or not
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Path to a file. |
True if the path is absolute and false if the path is relative.
Determines if a path is relative or not
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Path to a file. |
True if the path is relative and false if the path is absolute.
Determines the extension of a file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
Extension of the file including the "dot". Empty path is returned if no extension is found.
Changes the extension of a filename.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
||
character(len=*), | intent(in) | :: | ext |
New extension for the fname. |
The fname value with the modified extension.
Determines the filename without the path and without the final extension given a path.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
Filename without the final extension and without the path. If filename consists of an extension only, the extension is returned.
Determines the full filename given a path.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
Full filename given in the path.
Changes the filename of a path (keeping the path).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
||
character(len=*), | intent(in) | :: | newname |
New filename for the fname (including extension). |
The fname value with the modified filename.
Removes the filename from a path.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
The fname value with the filename removed.
Determines the path to the parent directory given the path to a file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fname |
Filename or path to a file. |
Path of the parent directory without final slash.