Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to identify a word in a text file while the file is written? 1

Status
Not open for further replies.

angel84

Programmer
Oct 5, 2012
6
IT
I have this problem. I have a big code which launches an external operation and i need to wait until this operation is finished.

I have a text file which gives me information about this external procedure. When a certain word is written in that file, then i know that the procedure has been terminated and that i can continue to execute the next operation.
The further complication is that the external procedures writes the code while it runs, therefore, it can happen that i read the text file too early and that the magic word has not already been written in the text file, therefore i need to read the text file multiple times until the magic word has been written.

This is the prototype that i wrote.

5 open(unit=123,file='textfile.text')
10 read(123, '(A)', end = 20) line
ind=index(line,'magic word')
if (ind .ne. 0) then
write(*,*)
print *, line
goto 30
endif
goto 10
20 close (123)
goto 5
30 continue
close (123)

This code acts like this: it scans the file 'textfile.text' looking for the magic word, and, if it doesn't find it, it reads the file till the end, closes it and opens it again to keep looking for the word. When it finds the word, it stops looking for the magic word, it closes the file and goes to the next operation.

This code worked fine for the first 2283 iterations, but then it failed, can someone tell me why it failed?

The error that i received is:

At line 355 of file rfi_na.F
Fortran runtime error: End of file

Line 355 is: read(111,*) name1, name2, misfitval

The error arises because the 111 file is empty. It is a consequence of something bad happened in the previous lines, in the part of the code written above.

Any suggestion would be greatly appreciated,


Angelo Sajeva,
PhD student, Pisa, Geophysics
 
I don't understand why you need to read the file more than once. IMO when you use for example the system() function to execute external program, then your program should wait until external program ends. But maybe I'm wrong...

To see what happens try to code IOSTAT in your read statement
Code:
integer :: stat
...
10 read(123, '(A)', end = 20, iostat=stat) line
...

Maybe when you tried to read the file you was already at the end of the file - then try to code REWIND before the READ in the loop:
Code:
integer :: stat
...
10 rewind(123, iostat=stat)
read(123, '(A)', end = 20, iostat=stat) line
...
 
Thank you very much, i will follow your suggestions. I changed the code according to your advice. And i will make it run now. It takes approximately 7 days to run, so i will have to wait for a while to know if this version of the code works. I hope it will. I will write here the news (hopefully happy ones) in the next days.

Regarding system() and its ability to wait for the external program to finish, i think that this do not apply when one works with a load sharing facility (LSF) to launch jobs, as is my case. In fact, if I use system to launch a batch job in fortran

call system('bsub < jobinstructions.txt')

the program waits until the job is admitted in the queue of the LSF system and then moves on to the next operation of the fortran code. It is like if the LSF and the fortran code are two persons who share the work, the fortran code starts working and then he calls LSF and gives him the job, now the job is on the hands of LSF, fortran is free and he keeps doing his stuff.
 
The corrected code should be

rewind(123, iostat=stat)
10 read(123, '(A)', end = 20,iostat=stat) line

otherwise the program reads only the first row.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top