Search code examples
fortranfortran77

How do I read data from a file with description and blank lines with Fortran 77?


I am new to Fortran 77. I need to read the data from a given text file into two arrays, but there are some lines that either are blank or contain descriptive information on the data set before the lines containing the data I need to read. How do I skip those lines?

Also, is there a way my code can count the number of lines containing the data I'm interested in in that file? Or do I necessarily have to count them by hand to build my do-loops for reading the data?

I have tried to find examples online and in Schaum's Programming with Fortran 77, but couldn't find anything too specific on that.

Part of the file I need to read data from follows below. I need to build an array with the entries under each column.

    Data from fig. 3 in Klapdor et al., MPLA_17(2002)2409

    E(keV)  counts_in_bin

    2031.5  5.4

    2032.5  0

    2033.5  0

I am assuming this question is very basic, but I've been fighting with this for a while now, so I thought I would ask.


Solution

  • If you know where the lines are that you don't need/want to read, you can advance the IO with a call to read with no input items.

    You can use:

    read(input-unit,*)
    

    to read a line from your input file, discard its contents and advance IO to the next line.