Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminating reading of .txt file

Status
Not open for further replies.

redglitter

Programmer
Sep 11, 2014
2
SG
Hey everyone!

I'm new to fortran and would like to get some help from you pros out there! It would really help a bunch.

Basically, my problem is that I have a .txt file, of unknown number of rows, which contains data in the following format:

**start**
equipment(a), value1, value2, value3, value4
equipment(b), value1, value2, value3, value4
equipment(c), value1, value2, value3, value4
.
.
.
End
**end**

I have no problems reading the data I need using the following code in a DO loop:
READ (1,*) equipment, a, b, c, d

However, I get an error when I want to terminate reading the .txt file. I'm currently using this syntax to exit:
IF (equipment=="END") EXIT

Are there any ways to terminate upon reading the "END" line? Your help is much appreciated! Thank you :)
 
for example something like this:

Code:
...
  integer :: stat
  ...
  ! open input file
  open(10,file=[i]'my_file.txt'[/i],status='old')

  do
    read(10,*,[highlight]end=99[/highlight], iostat=stat) [i]my_val01, my_val02, ...[/i]
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    call [i]process_my_data[/i]
  end do

  ! close file
  [highlight]99 continue[/highlight]
  close (10)
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top