Skip to main content

File Utilities

Helper functions for file I/O operations.

file_exists

Checks whether a file exists on disk.

logical :: exists
exists = file_exists(filepath)
ParameterIntentTypeDescription
filepathincharacter(*)File path

Returns: .true. if the file exists. Prints a warning if not found.

numRows

Counts the number of lines in an open file unit.

integer :: nrows
nrows = numRows(fileunit)
ParameterIntentTypeDescription
fileunitinintegerFortran file unit number (file must be open)

Returns: Number of lines. Rewinds the file unit after counting.

countkeys

Counts bracket-delimited keys [key] in a header file.

call countkeys(ifile, nkeys)
ParameterIntentTypeDescription
ifilecharacter(*)Path to the header file
nkeysintegerCounter (accumulated, must be initialized before call)

readheader

Parses a header file with [attribute] / content pairs.

call readheader(hfile, attribute, content)
ParameterIntentTypeDescription
hfileincharacter(*)Header file path
attributeoutcharacter(*), allocatable(:)Attribute names (without brackets)
contentoutcharacter(*), allocatable(:)Content values

Header File Format

[attribute_name]
attribute_value

[another_attribute]
another_value