j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
175 stars 14 forks source link

Add num_records and num_empty_records options to INQUIRE for formatted files. #305

Open rweed opened 1 year ago

rweed commented 1 year ago

One issue thats faced in trying to read formatted text files is determining the number of records (lines) in the file and the number of possible empty records prior to actually reading the file. The user is required to write their own loops and test for EOF and/or EOR conditions while keeping a count of the number of lines read and if a record is empty. This means the user has to loop through the file twice. To me counting the number of records in a formatted file and the number of empty records is something that should be done by INQUIRE since the compiler developers could (hopefully) write more efficient (faster) and safer code than the average user can. Therefore I propose adding two new optional arguments to INQUIRE that return the number of records and the number of empty records in formatted files. ie. something like

integer :: nrecs, nempty INQUIRE(file="myfile.txt", form="formatted", num_records=nrecs, num_empty_records=nempty)

Note this would only apply to formatted text (although you might be able to use it for stream access if you write your own EOR marks but that might be overcomplicating things). Since these are new options legacy code would not be affected.

klausler commented 1 year ago

This can be implemented in user libraries just as well as it could be in a runtime I/O support library.