Imagine the following trivial dataset:
A A 1 BBB 2
I only want to read the numbers, but I need to read them in unformatted, and the leading text is of a consistent length, but is not a consistent number of words. I may not always want to read the first line.
Consider the following program to read the data. It skips line 1, and then reads the entire line to work out how long the leading string is (not shown). It then attempts to backspace to read line 2 again, but this time to skip over the text and then read in the data.
PROGRAM p
REAL :: d
CHARACTER(LEN=64) :: c
!
OPEN (UNIT=11,FILE='external_file.txt',ACTION='read',FORM='formatted',STATUS='old')
READ (UNIT=11,FMT=*)
READ (UNIT=11,FMT='(A)') c
! ... some calculations to determine that '(A3)' will be adequate for skipping the leading string
BACKSPACE (UNIT=11)
READ (UNIT=11,FMT='(A3)',ADVANCE='no') c
READ (UNIT=11,FMT= *) d
PRINT *, d
END PROGRAM p
This all works great, except if the line used to diagnose the length of the string is the last line in the file. If it is, using BACKSPACE works as long as the last line ends with a carriage return, but if it does not then BACKSPACE takes you a line earlier. Try reading the data at the top of this post in these two ways. Without a carriage return at the end of line 2, the program reads in line 1; with a carriage return it reads in line 2.
As far as I can tell, that's how it should be (FTN95 is doing what the standard says), but it would still be nice to know where I am in the file after invoking BACKSPACE. IOSTAT returns a zero in both cases. There are of course work-arounds such as reading the data from c rather than backspacing at all, but I don't even know whether c has been long enough to read in the entire line of data. I could rewind and get back to this point again, but it may have taken a long time to find this location.
Anybody have any good ideas?[/code]