Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran95 datafile extraction help

  1. Jul 26, 2008 #1

    eth

    User Avatar

    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 wont 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 (Text):

       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 (Text):

          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!
     
  2. jcsd
  3. Jul 26, 2008 #2

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    Look for an End of file (EOF) character.

    My other suggestion is to use a while-loop.......
     
  4. Jul 26, 2008 #3

    eth

    User Avatar

    eek, can someone offer further explanation?

    what should it be "while"ing for?


    btw i'm using
    gfortran
    ubuntu 8.04

    if anyone needed to know that...
     
  5. Jul 26, 2008 #4

    rcgldr

    User Avatar
    Homework Helper

    Read() has an optional 3rd parameter. Since not all Fortran compilers have while loops I used goto:

    Code (Text):

          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 (Text):

          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: Jul 26, 2008
  6. Jul 26, 2008 #5

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    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 (Text):

    while ( x < 10 ) { // While x is less than 10
        cout<< x <<endl;
        x++;             // Update x so the condition can be met eventually
      }
     
     
  7. Jul 27, 2008 #6

    eth

    User Avatar

    Thanks all, I got it working. I was just missing the end= command.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Fortran95 datafile extraction help
  1. Fortran95 HELP (Replies: 5)

  2. Error 112 FORTRAN95 (Replies: 1)

Loading...