Fortran95 datafile extraction help

In summary, the fortran code attempts to read a file that is too large to fit into memory, and crashes the program at the end. The C++ code uses a while loop to read the file until it reaches the end.
  • #1
eth
3
0
Hello,

I'm pretty new to fortran95 and I've run into a stuck point extracting data from a file, here's what's up:

I have a file test.dat that has 2 columns and variable number of rows (different experiments will yield different amounts of points). I want to put the data into a matrix of equivalent dimensions as the file, if possible. My main issue is that since I won't be able to know the number of rows in the datafile, the "read" command crashes the program at the end of the file-reading.

datafile looks like this, with around 50000 rows:
Code:
   2        14.2000
   1        17.9000
   1        27.7000
   2        28.8000
   1        44.3500
   2        43.5000
   2        58.2500
   1        58.3500
   1        72.6700
   2        72.6500

The fortran code I have right now looks like:

Code:
      double precision datamatrix(200000,2)
      open(unit=9,file='data/test.dat')
      read(9,901) datamatrix
  901 format(F10.4,F10.4)

I think I'm in need of a command that finds how many rows are in the textfile

help please! Thanks!
 
Technology news on Phys.org
  • #2
Look for an End of file (EOF) character.

My other suggestion is to use a while-loop...
 
  • #3
Dr Transport said:
Look for an End of file (EOF) character.
eek, can someone offer further explanation?

Dr Transport said:
My other suggestion is to use a while-loop...
what should it be "while"ing for?


btw I'm using
gfortran
ubuntu 8.04

if anyone needed to know that...
 
  • #4
Read() has an optional 3rd parameter. Since not all Fortran compilers have while loops I used goto:

Code:
      integer i,n

      i = 0
100   i = i+1
      read(9, 901, END=101) datamatrix[i,1],datamatrix[i,2]
901   format(F10.4,F10.4)
      goto 100

101   n = i

or

Code:
      integer i,n
      integer sts

      i = 0
100   i = i + 1
      read(9, 901, IOSTS=sts) datamatrix[i,1],datamatrix[i,2]
901   format(F10.4,F10.4)
      if(sts .NE. 0) goto 101
      goto 100

101   n = i
 
Last edited:
  • #5
While loops will go until you reach the exit condition and are the preferred way to read a file where you don't know how long it is, which is what you indicated in the original post.

as pseudo code

while (not end of file)
read line into matrix
end of while

in C++ it looks like

Code:
while ( x < 10 ) { // While x is less than 10 
    cout<< x <<endl;
    x++;             // Update x so the condition can be met eventually
  }
 
  • #6
Thanks all, I got it working. I was just missing the end= command.
 

FAQ: Fortran95 datafile extraction help

What is Fortran95 datafile extraction?

Fortran95 datafile extraction is a process of retrieving data from a file using the Fortran95 programming language. This is commonly used in scientific and engineering applications for data analysis and manipulation.

What are the benefits of using Fortran95 for datafile extraction?

Fortran95 is a high-level programming language specifically designed for scientific and engineering computations. It offers efficient and fast processing of large datasets, making it ideal for datafile extraction tasks. It also has built-in data manipulation and analysis capabilities, making it a comprehensive tool for scientific data processing.

How do I extract data from a Fortran95 datafile?

To extract data from a Fortran95 datafile, you need to use the READ statement. This statement allows you to read data from a file into variables specified in your program. You can also use the OPEN statement to open the datafile and the CLOSE statement to close it after extraction.

Can I extract data from multiple files using Fortran95?

Yes, you can extract data from multiple files using Fortran95. You can use a loop structure, such as DO or DO WHILE, to read data from each file sequentially. Alternatively, you can use the INQUIRE statement to get information about the files and then use the OPEN and CLOSE statements to extract data from each file.

Are there any resources available for learning Fortran95 datafile extraction?

Yes, there are many online tutorials, forums, and documentation available for learning Fortran95 datafile extraction. You can also refer to books and online courses specifically designed for scientific programming in Fortran95. It is also helpful to consult with experienced Fortran programmers or attend workshops and conferences related to this programming language.

Similar threads

Replies
5
Views
4K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
5
Views
1K
Replies
12
Views
2K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
7
Views
1K
Replies
2
Views
2K
Replies
1
Views
3K
Back
Top