Fortran How to read data from various files of different sizes in fortran

AI Thread Summary
To read data from files of unknown size in Fortran, utilize the IOSTAT clause in the READ statement to determine when the end of the file is reached. By storing the read status in a variable, you can implement a loop that continues reading until an error status indicates the end of the file. An example approach involves opening the file, initializing a sum variable, and using a loop that reads data while checking the IOSTAT value. If the value indicates an error, the loop exits, allowing for proper handling of the data read. This method simplifies the code by reducing the need for nested if-statements and allows for efficient processing of variable-length input files.
SAMANDEEP
Messages
1
Reaction score
0
In my Fortran program, I want to read data from various files whose size I do not know in prior. The input files which I am trying to read may contain different number of entries. If I have same and known number of entries in all files (say 386), I can use something like:


open(unit=1,file='mu.dat',status='old')

open(unit=2,file='md.dat',status='old')

open(unit=3,file='ms.dat',status='old')

do i=1,386,1
read(1,*)mu(i)
read(2,*)md(i)

Can someone please help me in doing it if I don't know the number of rows in my data files in prior?

Thanks,
Saman.
 
Technology news on Phys.org
Is there some sort of marker to tell the code that the end of file has been reached?
 
Use an IOSTAT clause in your READ statement to store the read status (which indicates when you have reached the end of file) into a variable; then test that variable to decide whether to exit your input-loop.

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/iostatus.html

Just as a matter of personal taste, I think the if-statement nested inside the do-loop is a bit clunky, in the examples on that page. Here's how I'd do the second example (reading from a file on unit 4, instead of from *):

Code:
open (unit=4, file='mydata.dat', status='old')
sum = 0

read (4, *, iostat=io) x
do while (io == 0)
    sum = sum + x
    read (4, *, iostat=io) x
end do

if (io > 0) then
    write (*, *) 'Check your data file.  Something was wrong.'
else
    write (*, *) 'The total is ', sum
end if

Or you can use an EXIT to get out of the loop, then you need only one READ statement:

Code:
do
    read (4, *, iostat=io) x
    if (io /= 0) exit
    sum = sum + x
end do
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
1K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
6
Views
2K
Replies
2
Views
3K
Back
Top