Fortran95 datafile extraction help

  • Context: Fortran 
  • Thread starter Thread starter eth
  • Start date Start date
  • Tags Tags
    Extraction Fortran95
Click For Summary

Discussion Overview

The discussion revolves around extracting data from a file in Fortran95, specifically addressing the challenge of reading a variable number of rows from a data file without knowing the total number of rows in advance. Participants explore various methods to handle end-of-file conditions during data reading.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes their issue with reading data from a file and the program crashing when reaching the end of the file.
  • Another participant suggests looking for an End of File (EOF) character as a potential solution.
  • A follow-up question seeks clarification on how to implement a while-loop for reading the file, indicating uncertainty about its application.
  • One participant proposes using a loop with a goto statement to read data until EOF is reached, providing a code snippet as an example.
  • Another participant mentions using the optional third parameter in the read() function to handle end-of-file conditions, also providing a code example.
  • Discussion includes a brief mention of while loops as a preferred method for reading files of unknown length, with a comparison to C++ syntax.
  • A participant later confirms they resolved their issue by using the end= command in their code.

Areas of Agreement / Disagreement

Participants express various methods to handle the EOF condition, but there is no consensus on a single approach. Multiple competing views on how to implement the reading process remain present throughout the discussion.

Contextual Notes

Some participants reference specific Fortran features and coding practices, indicating a reliance on compiler-specific behavior and potential limitations in compatibility across different Fortran compilers.

eth
Messages
3
Reaction score
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
Look for an End of file (EOF) character.

My other suggestion is to use a while-loop...
 
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...
 
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:
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
  }
 
Thanks all, I got it working. I was just missing the end= command.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K