Speculative file access

Speculative file access

Hello everyone,

Does anyone have any suggestions as to the best way to perform speculative file access?

I'll give an example: I have a file saved from FORTRAN. I want to add a version number as the first entry in the file. OK, so my standard unformatted WRITE can do that, but now I have some files with a version number, some without. What READ can I do to perform the following logic:

Read first entry - is it a version number?
If NO - use file reading routine A
If YES - use file reading routine B

Obviously, once read I'd save it out with a version number.

Thanks for you help,


2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The "best" way to do this involves avoiding a rewind or backspace of the file if at all possible, in order to save time.

Read the first record into a character-string buffer, and test it:
If it is a version number, treat it as such, and skip to a new read statement for the next and subsequent records which will read directly into the usual target locations;
If it is not a version number, read from the character-string buffer into the usual locations for the first "real" record, take action regarding missing version number (set flag to indicate need to rewrite file, then write version number and first record), then skip to a read for the next and subsequent records, again directly into the target locations.

I have used this approach for formatted reads, but it should work for unformatted

Leave a Comment

Please sign in to add a comment. Not a member? Join today