2

I have an existing Fortran 77 program where the input values are read from an input file.

read (unit=*, fmt=*) value

The read statement automatically jumps to the next line in the file every time it is called.

Is it possible to replace the "reference" index with another data container, like an array?

For example:

read (myarray, fmt=*) value

I tried it but it always reads the first array-element and does not jump automatically to the next element.

I would have to change every read(unit=*, ...) to read(array(i), ...) and increase the i separately to get to the next element.

Since the program is huge, I am looking for a way to keep the existing read statements and just change the source of the data.

So the unit wouldn't be a integer value but a array where every element is a line from my input file.

Does anybody have an idea?

I tried to discribe the problem in code: (the input_file.input ist just 15 lines with the numbers 1 to 15)

program FortranInput

implicit none

! Variables

integer :: i, inpid
character*130, dimension(100) :: inp_values
character*130 :: value

inpid = 20

! Open File
open(inpid, file='input_file.input')

! Read from file ------------------------------------------------
do i = 1, 15
    ! read always takes the next line in the file
    read(inpid,'(a130)') value

    ! write each line to new array-element
    inp_values(i) = value

    ! output each line from file to screen
    write(*,*) value
end do

close (inpid)



! Read from array -----------------------------------------------

do i = 1, 15

    ! read always takes the first line in the array
    read(inp_values,'(a130)') value

    write(*,*) value

end do 

end program FortranInput
5
  • Welcome, please take the Welcome tour and read How to Ask. It will be good to learn the formatting features of the question editor to make your questions more readable. Commented Apr 23, 2018 at 11:26
  • I am sorry, but the code you show us is not nearly sufficient. You have to tell us how are those arrays declared and what data-type they are. Are they character strings? Also show us the code where you actually use the read statement. Can you do more reading in one statement instead of the loop? An implied-do perhaps? We have to see more code. Commented Apr 23, 2018 at 11:29
  • Hi Vladimir, you are right, I find it very difficult to explain my problem.. Sorry! I edited the post an added some code. Basically my problem is that if I read from a file the READ-Statement always jumps to the next line and if I read from an array it does not. Commented Apr 23, 2018 at 12:25
  • I would like to keep all my READ Statements in the code that currently read directly from the input-file (integer :: inpid) and change the data-type of the inpid to an array (character, dimension :: inpid) Commented Apr 23, 2018 at 12:29
  • if the goal is to keep the code the same as possible, you can write the array to a file and read it back.. Commented Apr 23, 2018 at 14:02

2 Answers 2

2

Yes, in your example you have to always read from the appropriate array element using the (i) syntax. I can't see another way.

However, often you can use a character array as file in multiple records without using the element index. Consider this:

integer :: i, n=15
character*130, dimension(100) :: inp_values
character*130 :: value
integer :: values(100)

do i = 1, n
    write(value,*) i

    inp_values(i) = value
end do

read(inp_values,'(*(i130,/))') values(1:n)

write(*,*) values(1:n)

or even

read(inp_values,*) values(1:n)

It is important to remember that an internal file does not keep track of the position at which it is opened. The position is only valid within each write or read statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to know about the character array.. but in my case I am afraid I have to change the code. Thank you very much!
0

Internal files, unlike external files, have no concept of persistent position (between input/output statements). In this regard if you want one read statement to transfer from one record and the next read from another record you will have to reference these records directly.

However, you don't show how you really want to use the input. If you can re-write the input to use a single read statement then the appropriate records will be the source.

For example, if you can rewrite

do i=1,5
   read(unit, '(I5)') x(i)
end do

as

read(unit, '(I5,/)') x(1:5)

then you can easily switch to using an internal file.

1 Comment

Thank you very much, I didn't know anything about internal and external files. In my case I will have to change the read-statements in the code and directly reference them. I'am very limitted to waht I can do with the input.

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.