Get format in which text file was written in FORTRAN

Asked

Viewed 130 times

1

Hello, I need to make a program that reads from an input file that has lines of text written in specific formats and processes the read information in order to create an output file with some data of interest. I know all the formats used to write the input file (they are described in a format file), but the data itself is written in it by another program, in a "random" way. Some lines are easy to identify (table end headers and markings, etc.) others are not. It would be much easier if somehow FORTRAN was able to identify the format in which each line read from the input file was written (e.g.: '(1X,I5,3F8.1,2(5A,1X))'). So I could just compare the format of the line with one of the formats of the list I have and extract almost directly the necessary data, for example knowing that the format of the line read and stored in the variable LINE is described in the variable FORMAT, could do something like:

IF (FORMATO='(1X,I5,3F8.1,2(5A,1X))') THEN
  READ(LINHA,'(6X,F8.1)') minha_variavel
END IF

Because there may be another read line format such as:

'(6A, 2F8.1, F8.6,2 (6A))'

That if I use the same READ command above, I will have a F8.1 variable written in "variable", but this will not be the value I wanted.

Does anyone know if FORTRAN has a function that does something similar (get the format in which a line of text file was written)? If yes, what is the function?

1 answer

2


Problem solved using a mix of some of the suggestions posted in the stack in English. I read each of the lines of the input file in an internal variable (RLIN) in the format '(A165)'. After that, I read all the contents of the string I put in this internal variable in several dummy variables, using the format I knew of the lines where I wanted to get some information (reading all line information in the desired format and having read with IOSTAT = 0 assures me that this is the correct line), then, if the reading result is ok (IOSTAT = 0), it is because the line I just read was the correct one for the information I wanted, then I store the contents of some of the dummy variables in variables that represent the values that interest me. In the code, the solution was more or less like this:

OPEN(UNIT=LU1,FILE=RlinName,STATUS='OLD')
ilin = 0
formato = '(14X,A,1X,F7.1,1X,F7.1,5X,A,1X,A,1X,A,5X,A,I5,1X,A,I3,3F8.1,A,A,A,1X,A,2(1X,F8.2),1X,A,1X,A)'
DO WHILE (.TRUE.)
  READ(LU1,'(A165)',END=300) RLINFILE
  READ(RLINFILE,formato,IOSTAT=linhaok) dum2_a1,dum2_f1,dum2_f2,dum2_a2,dum2_a3,dum2_a4,dum2_a5,dum2_i1,dum2_a6,dum2_i2,dum2_f3,dum2_f4,dum2_f5,dum2_a7,dum2_a8,dum2_a9,dum2_a10,dum2_f6,dum2_f7,dum2_a11,dum2_a12
  IF(linhaok.EQ.0) THEN
    ilin = ilin+1
    rlin_lshu(ilin) = dum2_a4
    rlin_nbpa(ilin) = dum2_i1
    rlin_ncir(ilin) = dum2_i2
    rlin_ppij(ilin) = dum2_f3
    rlin_pqij(ilin) = dum2_f4
    rlin_tapn(ilin) = dum2_a7
  END IF
END DO
300 CLOSE(UNIT=LU1)
  • More details on the Stack conversation in English: https://stackoverflow.com/questions/48563865/is-there-a-way-to-compare-the-format-in-which-a-line-of-a-text-file-was-written/48572692#48572692

Browser other questions tagged

You are not signed in. Login or sign up in order to post.