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

In summary, the speaker wants to know how to read data from unknown-sized files in their Fortran program. They suggest using an IOSTAT clause in the READ statement to determine when the end of the file has been reached and using an if-statement or EXIT to exit the input-loop. This will allow them to read data from files with different numbers of entries.
  • #1
SAMANDEEP
1
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
  • #2
Is there some sort of marker to tell the code that the end of file has been reached?
 
  • #3
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:

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

1. How do I read data from a file in Fortran?

Fortran has built-in functions for reading data from files, such as OPEN and READ. The OPEN function is used to open a file and specify its properties, while the READ function is used to read data from the file into variables.

2. Can Fortran read data from files of different sizes?

Yes, Fortran can read data from files of different sizes. It is important to properly specify the size and format of the data in the READ function to ensure that the data is read correctly.

3. How do I handle errors when reading data from files in Fortran?

Fortran has built-in error handling mechanisms, such as the IOSTAT variable, which can be used to check for errors during file operations. Additionally, you can use the ERR and END statements to handle errors and end the program if necessary.

4. Is there a limit to the size of files that Fortran can read?

The size of files that Fortran can read is dependent on the system's memory and the compiler's limitations. In general, Fortran can handle large files, but it is important to ensure that the system has enough memory to accommodate the file size.

5. Can I read data from non-text files in Fortran?

Yes, Fortran has the capability to read data from non-text files, such as binary files. However, the data must be properly formatted and the READ function must be used appropriately to ensure that the data is read correctly.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
790
Back
Top