Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

I/O formatting read data from a previous record

h_amini
Beginner
640 Views

Hi there

Is it possible to read data from a previous line?

For example my program needs to read the third line (30000) first, then the first line (10000) of the following data file:

01.txt:

10000

20000

30000

I can do it by closing and reopening the data file, but there may be a faster solution, e.g. a command for reading data from a specific line number.

INTEGER A,N

OPEN(unit = 1, file = '01.txt')

n = 2

READ (1,11)A

PRINT*, A

CLOSE(unit = 1)

OPEN(unit = 1, file = '01.txt')

n = 1

READ (1,11)A

PRINT*, A

CLOSE(unit = 1)

11 FORMAT(/,I<5>)

end

Any comment would be very much appreciated.

Regards

Hamid

0 Kudos
4 Replies
Les_Neilson
Valued Contributor II
640 Views

Look at BACKSPACE for getting the previous record,
and REWIND to go back to the begining of the file without needing to close and re-open it.

Les
0 Kudos
h_amini
Beginner
640 Views
Quoting - Les Neilson

Look at BACKSPACE for getting the previous record,
and REWIND to go back to the begining of the file without needing to close and re-open it.

Les

Many thanks.
0 Kudos
jimdempseyatthecove
Honored Contributor III
640 Views

Also consider

A2 = A1
A1 = A
READ(fmt) A


Then when A is your trigger (30000 in example) use A2

Use an array of prior results when the go-back is larger than a few.

Jim
0 Kudos
bmchenry
New Contributor II
640 Views
i would suggest tryinginternal reads into a character array and then simply setting the counter back 2 when special situations are encounteredto read prior records.
depending on how large your input dataset is, and how many times you need to check a prior value, you can save valuable time by simply inputting input in arrays (groups of 3) or more?
whenever you doing multiple open/close/rewind you are slowing things down.
memory is cheap and fast there's lots of it out there.

brian
0 Kudos
Reply