Riccardo Tobia
Riccardo Tobia

Reputation: 11

Recognise blank spaces inside an input file Fortran

I'm trying to develop a program in Fortran and I need it to recognize blank spaces inside the input file in order to jump to the next line if there is one for one particular value.

For example if the input is:

1.0 2.0 3.0 4.0
5.0     6.0 7.0
8.0 9.0 1.0 2.0
3.0 0.0 4.0 5.0

The program should write just the first, the third and the last line. Is it possible?

Upvotes: 1

Views: 348

Answers (1)

Eric
Eric

Reputation: 1472

You can read the file line by line and then look for spaces in the line:

program StackOverflowSandbox

implicit none

CHARACTER (LEN=15)             :: NextLine
LOGICAL                         :: OneFound
LOGICAL                         :: TwoFound
integer                         :: i
integer                         :: j

open(2, FILE = 'Input.txt')

SpaceFinder: DO i = 1, 4

    Read(2,'(A)') NextLine
    DO j = 1,15    
        if(NextLine(j:j).EQ.' ') then
              if(OneFound) then
                TwoFound = .TRUE.
                EXIT SpaceFinder
              else
                OneFound = .TRUE.
              end if
        else
            OneFound = .FALSE.
        end if        
    END DO               
END DO SpaceFinder

if(TwoFound) then
    Write(*,*) 'Found It!'    
end if

end program StackOverflowSandbox

Upvotes: 1

Related Questions