0

我正在尝试使用 FORTRAN 77 遍历文档中的所有行,并将特定行位置与字符串进行比较,然后对其进行编辑。

例如:

|BXK   |00640.3A  |AWP |1.01|
|BUCKEYE MUNICIPAL AIRPORT                                             |08794|

我想在第二行更改08794为。0871994

这是我到目前为止所拥有的:

       PROGRAM CONVERSION
    IMPLICIT NONE
    CHARACTER(LEN=120) :: ROW
    CHARACTER(LEN=2) :: DATE1='19', DATE2='20'
    INTEGER :: DATENUMBER
    INTEGER :: J

    OPEN(UNIT=1, FILE='BXK__96B.TXT', STATUS ='OLD')
    OPEN(UNIT=2, FILE='BXK__96B_MODIFIED.TXT', STATUS='UKNOWN')

    DO J=1,10000
    READ(1,'(A)') ROW
        IF (J==2) THEN
            DATENUMBER = ICHAR(ROW(76))
            IF ((DATENUMBER.LE.9) .AND. (DATENUMBER.GE.2)) THEN
                WRITE(2, '(A)' ROW(1:75), DATE1, ROW(76:120))
            ELSE 
                WRITE(2, '(A)' ROW(1:75), DATE2, ROW(76:120))
            ENDIF
        END IF
    END DO
    CONTINUE
    CLOSE(1)
    CLOSE(2)

    END
4

1 回答 1

0

Ahh, so what you mean is, you want to convert the 2-digit representation of the year found at the right end of line 2 into its 4-digit representation. You seem already to have figured out how to find the position of the leading digit of the year, ie 76. Rather easier than what you have written would be

integer :: year
.
.
.
read(line(76:77),'(i2)') year  ! this reads year from the characters in positions 76,77
if (20<=year.and.year<=90) then  ! not sure if this precisely your test
   year = year+1900
else
   year = year+2000
end if

write(line(76:79),'(i4)') year

I haven't gone to the trouble of integrating this into the rest of your code, that should be straightforward, if not ask for more help.

Actually, I suppose you probably haven't figured out how to find the column at which you want to start reading the year from line 2. Precisely how you do this depends on what the format of your file really is. The functions you need to familiarise yourself with are, as one of the comments tells you INDEX and SCAN.

If you are looking for the 4th character after the 2nd occurrence of | in line 2 you could do it this way:

integer :: posn_of_2nd_vertical_bar
.
.
.
posn_of_2nd_vertical_bar = scan(row(scan(row,'|')+1:),'|')

and then replace your constant 76 with posn_of_2nd_vertical_bar+4

于 2012-08-01T10:40:20.740 回答