Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Procedure to read whole line to deferred length character #595

Closed
awvwgk opened this issue Dec 12, 2021 · 0 comments · Fixed by #597
Closed

Procedure to read whole line to deferred length character #595

awvwgk opened this issue Dec 12, 2021 · 0 comments · Fixed by #597
Labels
easy Difficulty level is easy and good for starting into this project good first issue Good for newcomers idea Proposition of an idea and opening an issue to discuss it topic: IO Common input/output related features

Comments

@awvwgk
Copy link
Member

awvwgk commented Dec 12, 2021

Motivation

Just wrote a tutorial for using stdlib with fpm and noticed that I can't read a whole line into a deferred length character.

See: https://awvwgk.github.io/fpm-docs/en/tutorial/dependencies.html

Prior Art

For the tutorial I used the following snippet

  !> Read a whole line from a formatted unit into a deferred length character variable
  subroutine getline(unit, line, iostat, iomsg)
    !> Formatted IO unit
    integer, intent(in) :: unit
    !> Line to read
    character(len=:), allocatable, intent(out) :: line
    !> Status of operation
    integer, intent(out) :: iostat
    !> Error message
    character(len=:), allocatable, optional :: iomsg

    integer, parameter :: bufsize = 512
    character(len=bufsize) :: buffer, msg
    integer :: chunk, stat

    allocate(character(len=0) :: line)
    do
      read(unit, '(a)', advance='no', iostat=stat, iomsg=msg, size=chunk) buffer
      if (stat > 0) exit
      line = line // buffer(:chunk)
      if (stat < 0) then
        if (is_iostat_eor(stat)) stat = 0
        exit
      end if
    end do

    if (stat /= 0 .and. present(iomsg)) iomsg = trim(msg)
    iostat = stat
  end subroutine getline

Additional Information

Due to gcc#100875 and #354 I couldn't use string_type for the tutorial.

@awvwgk awvwgk added idea Proposition of an idea and opening an issue to discuss it topic: IO Common input/output related features easy Difficulty level is easy and good for starting into this project good first issue Good for newcomers labels Dec 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy Difficulty level is easy and good for starting into this project good first issue Good for newcomers idea Proposition of an idea and opening an issue to discuss it topic: IO Common input/output related features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant