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

  • Context: Fortran 
  • Thread starter Thread starter SAMANDEEP
  • Start date Start date
  • Tags Tags
    Data files Fortran
Click For Summary
SUMMARY

This discussion addresses reading data from files of unknown sizes in Fortran, specifically using the IOSTAT clause to manage end-of-file conditions. The user, Saman, seeks guidance on handling variable-length input files. The solution involves using the IOSTAT variable to determine when to exit the reading loop, allowing for dynamic file reading without prior knowledge of the number of entries. The provided examples illustrate effective implementations of this approach, enhancing the robustness of Fortran file handling.

PREREQUISITES
  • Understanding of Fortran programming language syntax
  • Familiarity with file handling in Fortran
  • Knowledge of the IOSTAT clause in Fortran READ statements
  • Basic programming concepts such as loops and conditionals
NEXT STEPS
  • Research the Fortran IOSTAT feature for error handling in file operations
  • Explore advanced file handling techniques in Fortran, including formatted and unformatted I/O
  • Learn about dynamic memory allocation in Fortran for handling variable-sized data
  • Investigate best practices for error checking and debugging in Fortran programs
USEFUL FOR

This discussion is beneficial for Fortran developers, data analysts working with Fortran, and anyone involved in scientific computing requiring efficient file input handling.

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:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K