Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28593 Discussions

read nth line from a file and put it to a variable

john1981
Beginner
1,408 Views
Hi every body
I have an input file thatshould be read20 times.
first timemy input file has one line.(my variableis locatedon first line and should be read )
second timemy input file hastwo lines.(my variable is located onsecond line and should be read )
thirdtime my input file has three lines.(my variable is located onthird line and should be read )
forthtime my input file hasfour lines.(my variable is located onforth line and should be read )
.
.
,
twentith time,my input file hastwenty lines.(my variable is located ontwentith lines and should be read )

for example:
* first time

Open(Unit=765,File='C:\\Temp\\disp3.dat'

* ,Action='read',Status='old',position='asis')

read(765,'(/1x,E12.5)')scx3

* second time ( next execution)

Open(Unit=765,File='C:\\Temp\\disp3.dat'

* ,Action='read',Status='old',position='asis')

read(765,'(/1x,E12.5)')scx3
.
,
,
but It didnt work. Please tell me how I solve my problem.
my input file after execution is sth like that each line should be read each time:

0.0000E+00

0.0000E+00

0.0000E+00

0.0000E+00

-4.1723E-07

3.5763E-07

4.1723E-07

1.5046E-01

2.7354E-01

1.2248E-01

1.5009E-01

1.5148E-01

1.3132E-01

1.2248E-01

1.5009E-01

1.5148E-01

1.3132E-01

4.1723E-07

1.5046E-01

2.7354E-01

1.2248E-01

Thanks in advance.
Regards,John

0 Kudos
5 Replies
mecej4
Honored Contributor III
1,408 Views
Why don't you read all twenty values in one gulp, after opening the file just once?

Why do your input formats have '/' in them? That will cause a line to be skipped.

Why does your input data span 21 lines?
0 Kudos
john1981
Beginner
1,408 Views

Dear mecej4
hi
As I said, my input file has 1 line in first time ( 1s), so I read first line.
second time ( 2s) my input file has2 lines, so I want to read justsecond line.
thirdtime ( 3s) my input file has3 lines, so I want to read justthird line.
.
.
.
so now tell me how I can read just one line of my file.
Thanks in advance.

0 Kudos
mecej4
Honored Contributor III
1,408 Views
Let us suppose that in your program the value of n is known (when the n-th line is to be read), and that at this time the file does contain at least n lines, and that the data file has records of length=11.

open (uu,file=<file_name>,form='formatted', &
access='direct',status='old',recl=11)
read (uu, fmt='(E10.4)', rec=n) x

will read the real number on line n into the variable x.



0 Kudos
anthonyrichards
New Contributor III
1,408 Views
You are reading from a file that is being modified after each read by adding a new record?
Is the same program writing to the file as is reading it, or is it being modified elsewhere?

If your 'read' program is also 'writing' the new record, then the file will be positioned after the latest write, so long as you do not CLOSE and reOPEN the file between your last read and the next write. Then, if you want to read the last record written, just use BACKSPACE on the unit to which the file is connected. However, if you close the file after reading, then reOPen it and then APPEND the next written record, the situation should be just the same and a BACKSPACE will position the file just before the last record appended, ready to read it.
0 Kudos
john1981
Beginner
1,408 Views
dear anthonyrichards
hi
Thanks for your reply.
My input is come from python, As I know python better than fortran.so,I change my input file and it has
one line each time. Now I have solved my problem.
Thanks for your support.
Regards,
John.
0 Kudos
Reply