ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_io::getCountRecordLeft Interface Reference

Generate and return the number of records left (starting immediately after the last accessed record) in the record-oriented input file. More...

Detailed Description

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)
use pm_kind, only: LK, SK
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...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
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.
  1. If .true., the record position in the file will be reset to where it was upon entering the procedure.
  2. 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.
  1. If present and no error occurs, it is set to 0 on output.
  2. 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.
  3. 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) ...
Definition: pm_io.F90:2226
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
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

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_io, only: display_type
6
7 implicit none
8
9 integer(IK) :: unit
10 character(255, SK) :: record
11 type(display_type) :: disp
12
13 disp = display_type(file = "main.out.F90")
14
15 call disp%skip
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("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
19 call disp%skip
20
21 call disp%skip
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)")
25 call disp%show( getCountRecordLeft(unit) )
26 call disp%show("close(unit)")
27 close(unit)
28 call disp%skip
29
30 call disp%skip
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("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
34 call disp%skip
35
36 call disp%skip
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)
43 call disp%show("trim(record)")
44 call disp%show( trim(record) , deliml = SK_"""" )
45 call disp%show("getCountRecordLeft(unit, reset = .true._LK)")
46 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
49 call disp%show("trim(record)")
50 call disp%show( trim(record) , deliml = SK_"""" )
51 call disp%show("close(unit)")
52 close(unit)
53 call disp%skip
54
55 call disp%skip
56 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
57 call disp%show("!Exclude certain records that match the user-specified behavior via isCountable.")
58 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
59 call disp%skip
60
61 call disp%skip
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)")
65 call disp%show( getCountRecordLeft(unit, isCountable = isCountable) )
66 call disp%show("close(unit)")
67 close(unit)
68 call disp%skip
69
70contains
71
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
76 end function
77
78end program example
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Count the number of records in the record-oriented (sequential access) `main.F90` file.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')
9+77
10close(unit)
11
12
13!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14!Count the number of records in the `main.F90` file starting from the current position, then reset the position to where it was.
15!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17
18open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')
19read(unit,*); read(unit,*) ! move the position to the beginning of the third record.
20read(unit,'(a)') record; backspace(unit) ! read the current line as a reference to compare.
21trim(record)
22" use pm_kind, only: SK, IK, LK"
23getCountRecordLeft(unit, reset = .true._LK)
24+75
25read(unit,'(a)') record ! read the current line again to compare with the value before calling getCountRecordLeft().
26trim(record)
27" use pm_io, only: display_type"
28close(unit)
29
30
31!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32!Exclude certain records that match the user-specified behavior via isCountable.
33!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35
36open(newunit = unit, file = 'main.F90', status = 'old', action = 'read')
37getCountRecordLeft(unit, isCountable = isCountable)
38+65
39close(unit)
40
41
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.

  1. 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.
  2. 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.

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.


The documentation for this interface was generated from the following file: