Generate and return the number of records left (starting immediately after the last accessed record) in the record-oriented input file.
If any error occurs during the IO and the output iostat
argument is missing, the program will halt by calling error stop
.
- Parameters
-
[in] | unit | : The input scalar integer of default kind IK containing the unit of the connected file whose number of record left is to be returned. |
| isCountable | : The external user-specified function that takes one input scalar character of default kind SK of arbitrary length type parameter, containing the most recent record that has been read from the input file.
It returns a scalar logical of default kind LK that is .true. if and only if the input record should be included in counting.
The following illustrates the generic interface of isCountable , function isCountable(record) result(countable)
character(*, SK), intent(in) :: record
logical(LK) :: countable
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter SK The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
This user-defined countability check is extremely useful where certain records in the file ought to be excluded from counting or only records with specific patterns should be included (for example, excluding or including only records that are empty or begin with or end with or have specific patterns).
(optional. If missing, all records are included in counting). |
[in] | reset | : The input scalar logical of default kind LK.
-
If
.true. , the record position in the file will be reset to where it was upon entering the procedure.
-
If
.false. , the record position will be the end of the file.
This option is useful for scenarios where a file has been partially read (e.g., fixed header), but the number of remaining file records is required to read the rest of the file.
This is done by calling the Fortran intrinsic backspace for nrecord number of times.
Note that the backspace statement is often very costly in computer resources.
Therefore this use of this optional argument should be minimized in performance critical routines.
(optional, default = .false. ) |
[out] | iostat | : The optional output scalar integer of default kind IK.
- If present and no error occurs, it is set to
0 on output.
- If present and an error occurs (e.g., if the input argument values are wrong or inconsistent), it is set to a positive non-zero value.
- If missing and an error occurs, then the program halts by calling
error stop followed by the relevant error message.
|
[in,out] | iomsg | : The input/output scalar character of default kind SK containing the error message, if any error occurs.
A length type parameter value of LEN_IOMSG is generally sufficient for iomsg to contain the output error messages.
(optional. Its presence is relevant only if the iostat output argument is also present.) |
- Returns
nrecord
: The output scalar integer
of default kind IK, representing the number of records in the input file.
Possible calling interfaces ⛓
nrecord
= getCountRecordLeft(file, isCountable
= isCountable, reset
= reset,
iostat = iostat,
iomsg = iomsg)
Generate and return the number of records left (starting immediately after the last accessed record) ...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
- Warning
- The input
unit
must be connected to a file prior to calling this procedure.
Additionally, this procedure will not close the file upon return.
- See also
- getCountRecord
isPreconnected
setRecordFrom
getFileUnit
Example usage ⛓
10 character(
255, SK) :: record
11 type(display_type) :: disp
16 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
17 call disp%show(
"!Count the number of records in the record-oriented (sequential access) `main.F90` file.")
18 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
22 call disp%show(
"open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')")
23 open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
24 call disp%show(
"getCountRecordLeft(unit)")
31 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
32 call disp%show(
"!Count the number of records in the `main.F90` file starting from the current position, then reset the position to where it was.")
33 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
37 call disp%show(
"open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')")
38 open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
39 call disp%show(
"read(unit,*); read(unit,*) ! move the position to the beginning of the third record.")
40 read(unit,
*);
read(unit,
*)
41 call disp%show(
"read(unit,'(a)') record; backspace(unit) ! read the current line as a reference to compare.")
42 read(unit,
'(a)') record;
backspace(unit)
44 call disp%show(
trim(record) , deliml
= SK_
"""" )
45 call disp%show(
"getCountRecordLeft(unit, reset = .true._LK)")
47 call disp%show(
"read(unit,'(a)') record ! read the current line again to compare with the value before calling getCountRecordLeft().")
48 read(unit,
'(a)') record
50 call disp%show(
trim(record) , deliml
= SK_
"""" )
56 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
57 call disp%show(
"!Exclude certain records that match the user-specified behavior via isCountable.")
58 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
62 call disp%show(
"open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')")
63 open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
64 call disp%show(
"getCountRecordLeft(unit, isCountable = isCountable)")
72 function isCountable(record)
result(countable)
73 character(
*, SK),
intent(in) :: record
74 logical(LK) :: countable
75 countable
= index(record, SK_
"call disp%skip",
kind = IK)
== 0_IK
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
7open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
18open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
19read(unit,
*);
read(unit,
*)
20read(unit,
'(a)') record;
backspace(unit)
22" use pm_kind, only: SK, IK, LK"
25read(unit,
'(a)') record
27" use pm_io, only: display_type"
36open(newunit
= unit, file
= 'main.F90', status
= 'old', action
= 'read')
- Test:
- test_pm_io
- Todo:
- This procedure can be converted to a generic interface to add the optional
iseq()
external comparison procedures for custom exclusion or inclusion of lines in the count.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, Tuesday March 7, 2017, 3:50 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 2226 of file pm_io.F90.