Fortran: Read data from a line in a file

  • Fortran
  • Thread starter sourish_SUNY
  • Start date
  • #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,
SC
 

Answers and Replies

  • #2
35,236
7,056
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.
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.
 

Related Threads on Fortran: Read data from a line in a file

Replies
7
Views
1K
Replies
4
Views
10K
Replies
5
Views
18K
Replies
5
Views
4K
Replies
2
Views
787
  • Last Post
Replies
2
Views
3K
Replies
5
Views
11K
Top