CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   Fortran: Seeking data in files. (

cfd guy April 26, 2002 11:08

Fortran: Seeking data in files.
Hi people,
I was wondering if I can extract a specific data from a file using a subroutine in FORTRAN. Which command can I use? Below, I'll show a simple example. Suppose that I have a file, TEST.DAT, and this file has the following lines:
. . . . . VAR = 10 VAR2 = 50 /* COMMENT */ TIME = 100 VAR3 = 30 . . . . .
I want to extract the value from TIME variable. How can I do that? Important, this variable doesn't remain constant in a certain line. Is there any function in FORTRAN that I can seek this characters ('TIME = ') and then READ it (100) in my subroutine? I hope anyone of you can help me.
Kind regards, cfd guy

Bob April 27, 2002 01:20

Re: Fortran: Seeking data in files.
It depends entirely on which Fortran compiler you are using. Each has their own string handling intrinsics. Try reading the manual or help file of yours under strings or intrinsic functions.

cfd guy April 27, 2002 13:09

Re: Fortran: Seeking data in files.
Thanks Bob,
I use Visual Compaq Fortran 6.5 under NT. I took a look at manuals several times and I didn't find a specific command for seeking strings in files.
cfd guy

andy April 28, 2002 08:02

Re: Fortran: Seeking data in files.
Native Fortran is good enough to handling this sort of thing directly. Read each line into a character buffer and then test/read it for whatever you want. Something like below should work (adding declarations and removing possible typos and the mangling by html).

1000 read( dn, '(a)', iostat=ierr ) buffer

if ( ierr .eq. 0 ) then

if ( buffer(1:6) .eq. 'TEST =' ) then

read( buffer(7:),'(i8)' ) time


goto 1000

end if

end if

Of course, you could generalize it a bit and put it in a function or subroutine. (Curiously, this was one of the in-class exercises I used to set students many years ago when teaching Fortran.)

peter.zhao April 28, 2002 09:55

Re: Fortran: Seeking data in files.
CFD guy:

You can use the namelist input, which can be found in help file!

Good luck!

cfd guy May 3, 2002 12:03

Re: Fortran: Seeking data in files.
Thank you very much andy,
Your native fortran suggestion worked very well.
Kind regards, cfd guy

wen long May 23, 2002 22:30

How to do it in Matlab?
Then how to do it in Matlab? Thanks!

All times are GMT -4. The time now is 23:20.