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

Fortran: Read data from a line in a file

  1. Sep 19, 2011 #1
    This is my problem:
    I would like to read data (saved in a text file) from a file. My data is written in a matrix format (1,000,000 rows x 3 columns).
    I want to read a data from a particular line, say row number 90,000.
    Since the number of rows is large, it will be very expensive if I have to do an empty read() for 89,999 rows.
    Is there a way I can directly go to row number 90,000 (without reading the lines before it) and read the corresponding data? I do have control over how the text file is created.
    Will really appreciate any help or advice on this.

    Thank You,
  2. jcsd
  3. Sep 19, 2011 #2


    Staff: Mentor

    I can't imagine that this would work very well for you. You're trying to perform random access on an object whose normal mode of reading is sequential access. Although it might seem that the data in your file is in matrix form, in reality, the data is in 1,000,000 million lines. It might be more realistic to read the data from the file, and store it in memory in an actual matrix (a two-d array). You didn't say what kind of data, so I can't say how much memory the matrix would use. If each line in your file consists of three double precision numbers, (at eight bytes each), the matrix would use about 24,000,000 bytes, or about 24 MB.

    It would take a while to read the data into memory, but once there, you could access any element in the 90,000 row pretty quickly.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook