BACKSPACE in FORTRAN
BACKSPACE in FORTRAN
specified unit number to just before the preceding record. For example,
`backspace(22)` would reposition the file connected to unit 22 to just before the
preceding record¹.
It's important to note that the `backspace` statement has no effect on a terminal
file and must be connected for sequential access. The execution of a `backspace`
statement on a direct-access file or an append access file is not defined in the
FORTRAN 77 Standard and is unpredictable¹.
######################
Yes, if unit 22 is associated with a text file, then the `backspace(22)` statement
would reposition that file to just before the preceding record. The file type
(e.g., `.txt`) does not affect the behavior of the `backspace` statement.
In the context of a text file, a record typically refers to a line of text. So, if
unit 22 is associated with a text file, then the `backspace(22)` statement would
reposition the file to just before the preceding line of text.
##########
does statement BACKSPACE require any other specifier beside the unit number ?
#########
The `BACKSPACE` statement in Fortran requires only the unit number as a mandatory
argument. However, it can also accept optional arguments such as `IOSTAT` and
`ERR`. The `IOSTAT` argument specifies an integer variable that will receive the
I/O status after the `BACKSPACE` statement is executed. The `ERR` argument
specifies a statement label to which control will be transferred in case of an
error during the execution of the `BACKSPACE` statement.
Here is an example that shows how these optional arguments can be used:
```
integer :: iostat
...
backspace(22, iostat=iostat, err=999)
...
999 write(*,*) 'Error during BACKSPACE'
```
In this example, the `iostat` variable will receive the I/O status after the
`backspace` statement is executed. If an error occurs during the execution of the
`backspace` statement, control will be transferred to the statement labeled `999`,
which writes an error message to the screen.
##############
can the err specifier also be used in READ statement ?
##############
Yes, the `ERR` specifier can also be used with the `READ` statement in Fortran.
Similar to its use with the `BACKSPACE` statement, the `ERR` specifier in a `READ`
statement specifies a statement label to which control will be transferred in case
of an error during the execution of the `READ` statement.
Here is an example that shows how the `ERR` specifier can be used with a `READ`
statement:
```
integer :: i
...
read(*,*,err=999) i
...
999 write(*,*) 'Error during READ'
```
In this example, if an error occurs during the execution of the `read` statement,
control will be transferred to the statement labeled `999`, which writes an error
message to the screen.