Skip to main content

ReadLine

This method reads a single line from a given text file.

Interface

INTERFACE
MODULE SUBROUTINE ReadLine(obj, val, iostat, iomsg, &
ignoreComment, ignoreBlank, commentSymbol, separator)
CLASS(TxtFile_), INTENT(INOUT) :: obj
TYPE(String), INTENT(OUT) :: val
INTEGER(I4B), OPTIONAL, INTENT(OUT) :: iostat
CHARACTER(*), OPTIONAL, INTENT(OUT) :: iomsg
LOGICAL(LGT), OPTIONAL, INTENT(IN) :: ignoreComment
LOGICAL(LGT), OPTIONAL, INTENT(IN) :: ignoreBlank
CHARACTER(1), OPTIONAL, INTENT(IN) :: commentSymbol
CHARACTER(*), OPTIONAL, INTENT(IN) :: separator
END SUBROUTINE ReadLine
END INTERFACE

Example 1

In this example we create an instance of TxtFile, then we open it and write some data to it.

! In this example we create an instance of
! TxtFile_, then we open it and write some data to it.
! Importing modules and defining variables

PROGRAM main
USE TxtFile_Class
USE String_Class
USE GlobalData
USE Display_Method

TYPE(TxtFile_) :: obj
TYPE(String) :: aline
INTEGER(I4B) :: ii

CHARACTER(len=*), PARAMETER :: filename = "./files/_ReadLine_test_1.txt"

! Initiate an instance of [[TxtFile_]], and then Open the [[TxtFile_]] file
CALL obj%Initiate(filename=filename, status='NEW', &
action='WRITE')
CALL obj%OPEN()

! Write a long line to the file
aline = aline%REPEAT("hello world! ", 3)
WRITE (obj%GetUnitNo(), "(A)") aline%chars()

! Lets close the file.

CALL obj%DEALLOCATE

! Lets open it again with read access.

CALL obj%Initiate(filename=filename, status='OLD', &
action='READ')
CALL obj%OPEN()

! Lets read the long line

aline = ""
CALL obj%ReadLine(aline)

CALL Display(aline, "aline read = ")

CALL obj%DEALLOCATE()

END PROGRAM main

Example 2

In this example we show how to use ReadLine function.

! In this example we show how to use `ReadLine` function.

PROGRAM main
USE TxtFile_Class
USE String_Class
USE GlobalData
USE Display_Method

TYPE(TxtFile_) :: obj
TYPE(String) :: aline
INTEGER(I4B) :: ii
CHARACTER(len=*), PARAMETER :: filename = "./files/_ReadLine_test_2.txt"

! Initiate an instance of [[TxtFile_]], and then Open the [[TxtFile_]] file

CALL obj%Initiate(filename=filename, status='OLD', action='READ')
CALL obj%OPEN()

! Read a line/record from the buffer
CALL obj%ReadLine(aline)
CALL Display(aline, "line "//tostring(1)//"=")

! Rewind the file
CALL obj%REWIND()

! Let us read the entire file.
ii = 0
DO WHILE (.NOT. obj%IsEOF())
CALL obj%ReadLine(aline)
ii = ii + 1
CALL Display(aline, "line #"//tostring(ii)//": ")
END DO

! Cleaning up.

CALL obj%DEALLOCATE()
END PROGRAM main