Eric
Eric

Reputation: 11

How to skip bytes in file read using stream

I'm trying to read some noncontiguous fields from a fixed length data using Fortran. I would like to read (stream) from a binary file an array of 4 byte integers each separated by 6 bytes (i.e. read 4bytes, skip 6bytes, read 4bytes, skip 6bytes,...). I could use a dummy variable of size 6bytes; however, I was wondering if there was an option in the read statement to skip bytes after reading each element of the array. Thanks in advance for you help.

Eric

Upvotes: 1

Views: 1195

Answers (1)

amicitas
amicitas

Reputation: 13651

I think what you are looking for is direct access files: Direct-access files. Here you specify the number of bytes per record in the the OPEN statement, then specify which record to read in your READ statement. In your case the record length would be 10 bytes and you would only use the first 4 bytes of each record.

Personally I would just use a dummy variable and a format statement. I think it would make the final code more clear and I don't think there would be any performance penalty.

Upvotes: 1

Related Questions