Skip to content

Query the number of lines in a text file conveniently #560

Open
@zoziha

Description

@zoziha

Motivation

I have met many novices who have no programming background and are mostly related to scientific computing. Fortran beginners often fail to master the reading of files well and will encounter EOF (End of File) errors.
If we can query the number of lines in the file in advance, we can slightly avoid EOF and guide the allocation of the allocatable array.

So, I see that there is already a function number_of_rows in stdlib_io, I think it can be overloaded:

interface count_lines
    module procedure :: number_of_rows
end interface count_lines

! - - -

counts = count_lines(unit)

Prior Art

Count lines:

Read the context of the file as a string (or array)

Additional Information

  • counts = count_lines(filename [, iostat] )
  • type(string_type) :: string(:)/string = read_file(unit/filename [, iostat] )
  • call count_lines( unit/filename, lines [, columns, max_width, len_lines(:), iostat] )
  • call count_lines( unit/file, lines [, max_width, iostat] )

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaProposition of an idea and opening an issue to discuss ittopic: IOCommon input/output related features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions