0

I have to read input data in my Fortran 77 program. It Looks as following

FILENAME PATH

temperature  ./Data

and then in the Fortran code I proceed as following

CHARACTER*8 FILENAME,PATH

READ(5,'(A80)') CARDIN 

READ(5,*) FILENAME,PATH

but the problem is that I don't know the length of FILENAME and PATH in advance. So what if the user doesn't give names that are not exactly 8? Do you have any idea how I can solve this?

3
  • 3
    Do you truly have a requirement to use/be restricted by Fortran 77? Commented Nov 16, 2016 at 9:17
  • Be aware of problems like this one when reading file paths (containing unquoted slashes) with list-directed input. Commented Nov 16, 2016 at 11:22
  • I suggest you read the ISO standard documentation for FORTRAN 77 on Hollerith constants or how assumed-size arrays apply to character types. I don't know of any way to achieve your goal without resorting to features from newer Fortran standards. Commented Nov 17, 2016 at 18:20

2 Answers 2

1

One can do it in F77, but this is more F90 in style.

INTEGER*4 FUNCTION LengthString(String)
IMPLICIT NONE
CHARACTER* String
INTEGER*4 I

LengthString = -1 !default is -1 just because.

DO I=LEN(String), 1, -1
  IF(String(I:I) .ne. ' ') THEN
     LengthString = I
     EXIT
  ENDIF
ENDDO

RETURN
END FUNCTION LengthString

And the program could be something like this:

PROGRAM Main
CHARACTER*80 AA
INTEGER*4 LenAA
...
LenAA = LengthString(AA)
IF(LenAA .lt. 1) !error out
...
WRITE(*,10) LenAA, A(1:LenAA)
10 FORMAT('AA(',I4,')="',A,'"')
...

Open statement could look like this:

OPEN(FILE=AA(1:LenAA),...

And the path is the same deal.

NewLongFile = Path(1:LenPath) // '/' // AA(1:LenAA)
Len_NewLongFile = LengthString(NewLongFile)
Sign up to request clarification or add additional context in comments.

4 Comments

What is objectionable about len_trim?
i see what you mean @francescalus ! I had rolled my own a long time ago... [strokes beard -n- chins]
To be fair, len_trim isn't F77 (but then, neither is enddo).
Your code does the job but does not answer the original question. @MorganeMaPh explicitly said that he/she must restrict themselves to FORTRAN 77. implicit none, len_trim for character manipulation, end do loop termination, and the exit statement were introduced in Fortran 90/95. Never mind the 6 character length restriction for naming conventions.
0

I'm not entirely sure what you want to do, but you can use TRIM to "cut" the blanc space:

      Program TEST
      CHARACTER*50 FILENAME,PATH

      READ(*,*) FILENAME,PATH
      WRITE(*,*)TRIM(FILENAME),TRIM(PATH)
      END

5 Comments

Nice use of TRIM. I usually use 80, 128, 256, or 1024. And then a seperate variable to understand the length. You will quickly find a function to find the end of the string can be useful even if you also use TRIM. And useful for a variety of flexible output handling of strings.
Thank you @PeMa could work... What I would like to do is write a Statement like "Read the string until you get a blanc space".
The problem is not in writing, but in reading. This is not a good solution.
Exactly @VladimirF ... also as I defined the variable I already have to give a length... this is bad
TRIM is not Fortran 77.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.