Quantcast
Channel: Intel® Fortran Composer XE
Viewing all articles
Browse latest Browse all 1424

Reading selective data from a sequential access file

$
0
0

My program needs to read data in from blocks deep inside of a sequential access file. It does not know a priori how long each block is, but the blocks are terminated by a line beginning with a -1 flag. The first block may have data to be read into array A1, the second block to be read into array A2, etc. The arrays are to be dynamically allocated to contain however much data is present in the file.

How can I do this?

If there were only one set of data records, my approach would be to read only the first number from each record, counting the no. of records until -1 is found. Then the array can be allocated, I then have a count of the number of records. Then I would allocate A1, REWIND the file, and begin reading again, this time storing all of the numbers on each line into the appropriate location in A1.

But when there are multiple blocks this method does not work so well. After the first read of the second block of records I cannot simply rewind the file; I need to back up the position by the no. of records in the second block, say NREC2 to start the second read. I could do this by DO I=1,NREC2; BACKSPACE; END DO. I have read that this is a waste of resources and not recommended.

I could also REWIND the file after the first readthrough of any block, then advance to the beginning of the desired block to start the 2nd read. This could be done using DO I=1,N; READ (*); END DO. Not much better.

I could do the first read through the ENTIRE data file, noting the record no. of each one of the -1 block-end flags and thus the no. of blocks and the amount of data in each, then allocate all of the arrays, then REWIND the file, and then begin reading the data. But this sequence is not always possible--sometimes I need to read the data from only a particular block in the middle of the file.

If I were processing a binary file I could make great use of the intrinsic (Portability) function FSEEK, which can move the position a specified no. of bytes from the current position. I wish there were a similar function that worked on records instead of bytes.

Ideas?


Viewing all articles
Browse latest Browse all 1424

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>