# IOSTAT and END within a READ statement

**URL:** https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455
**Category:** Help
**Created:** [December 28, 2021, 11:19am UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455 "2021-12-28T11:19:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![garynewport](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@garynewport](https://fortran-lang.discourse.group/u/garynewport)
#### Post date: [December 28, 2021, 11:19am UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455/1 "2021-12-28T11:19:23Z")

</div>

Asking for clarity, I have a series of read lines with END statements. These END statements act as GOTO statements, which I am slowly removing from the code.

I understand that IOSTAT detects EOF and file errors, so believe that I could simply switch out the END statements for the IOSTAT and then react to the IOSTAT value accordingly?

Am I correct in my thinking here?

---

<div class="post-metadata">

### Author: ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)
#### Post date: [December 28, 2021, 12:59pm UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455/2 "2021-12-28T12:59:03Z")

</div>

You are correct: a positive value of IOSTAT means something went wrong with reading the file, a negative value means you reached the end of the file (or the end of a record). Use the ISO\_FORTRAN\_ENV module to abstract away any specific values of the latter two: IOSTAT\_END and IOSTAT\_EOR.  
That said, if you have a lot of such statements in one unit, then it may still be useful to use the END= clause, as then you can gather all the error handling in a convenient spot in the code.

---

<div class="post-metadata">

### Author: ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)
#### Post date: [December 28, 2021, 1:33pm UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455/3 "2021-12-28T13:33:59Z")

</div>

To avoid goto’s and statement labels for error handling, labelled blocks are working quite nicely (if you don’t mind the additional indentation), we use a construct like this in stdlib:

> <https://github.com/fortran-lang/stdlib/blob/2bb6539fada37fe47039e5f3555b4047b0b45fbd/src/stdlib_io_npy_load.fypp#L34-L66>

Most handling of `iostat=stat` values happens in subroutines via return or in the main block via `exit catch` statements to leave the block and cleanup (close the unit and propagate the error).

---

<div class="post-metadata">

### Author: ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)
#### Post date: [December 28, 2021, 2:33pm UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455/4 "2021-12-28T14:33:41Z")

</div>

Since Fortran 2003, you can call the intrinsic logical functions `is_iostat_end(stat)` and `is_iostat_eor(stat)` to check for end-of-file and end-of-record instead.

---

<div class="post-metadata">

### Author: ![NormanKirkby](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/normankirkby/32/1091_2.png) [@NormanKirkby](https://fortran-lang.discourse.group/u/NormanKirkby)
#### Post date: [December 28, 2021, 2:56pm UTC](https://fortran-lang.discourse.group/t/iostat-and-end-within-a-read-statement/2455/5 "2021-12-28T14:56:21Z")

</div>

and the intrinsic logical functions do not require `use iso_fortran_env`
