correct counting of lines

counts like "wc -l" either with or without EOL in last line
This commit is contained in:
Martin Diehl 2020-06-01 22:09:28 +02:00
parent 2badf257fc
commit 37d7e593e8
1 changed files with 8 additions and 6 deletions

View File

@ -70,16 +70,13 @@ function IO_read_ASCII(fileName) result(fileContent)
startPos, endPos, & startPos, endPos, &
N_lines, & !< # lines read from file N_lines, & !< # lines read from file
l, & l, &
i, &
myStat myStat
logical :: warned logical :: warned
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
! read data as stream ! read data as stream
inquire(file = fileName, size=fileLength) inquire(file = fileName, size=fileLength)
if (fileLength == 0) then
allocate(fileContent(0))
return
endif
open(newunit=fileUnit, file=fileName, access='stream',& open(newunit=fileUnit, file=fileName, access='stream',&
status='old', position='rewind', action='read',iostat=myStat) status='old', position='rewind', action='read',iostat=myStat)
if(myStat /= 0) call IO_error(100,ext_msg=trim(fileName)) if(myStat /= 0) call IO_error(100,ext_msg=trim(fileName))
@ -89,10 +86,15 @@ function IO_read_ASCII(fileName) result(fileContent)
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
! count lines to allocate string array ! count lines to allocate string array
N_lines = 1 N_lines = 0
i = 0
do l=1, len(rawData) do l=1, len(rawData)
if (rawData(l:l) == IO_EOL) N_lines = N_lines+1 if (rawData(l:l) == IO_EOL) then
N_lines = N_lines+1
i = l
endif
enddo enddo
if(i+1/=l) N_lines = N_lines+1 ! no EOL in last line
allocate(fileContent(N_lines)) allocate(fileContent(N_lines))
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------