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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random read from file...HELP!

Status
Not open for further replies.

kostalibe

Technical User
Jun 16, 2008
5
GR
Hi,

I am new with fortran and i face a problem with reading a file with a huge number of rows. I want to read this file randomly and not read the data as they are stored in file.
The file is like this...
U[m/s] Dir[deg] Usdv[m/s] Umax[m/s] Time Date
7.8 233.6 0.5 9.4 0:02:00 1/9/1999

I use only the first column and i dont want to read it line by line, but randomly.

Thank you in advance,
Kostas
 
Do all the lines contain the same number of characters? If they do they you can open the file for direct access and specify record positions.

If they don't, you'll probably have to read it line by line but you can stop at the first number.
 
thanks for the reply

All the lines contain the same number of characters.
So can you please post an example of opening the file for direct access.

thanks a lot
 
Something like this. This example will open a file called fixlen.txt, each record is 20 chars long.

It will read records 1, 6, 11, 16 ...

On each record it will skip the first 6 chars and read the next 6 as an integer.
Code:
   integer, parameter:: stream = 20, streamlen=20
   integer, parameter:: rowmax = 50
   integer:: row, val
   
   open (stream, file='fixlen.txt', &
      form='FORMATTED', access='DIRECT',&
      recl=streamlen, status='old')

   do row = 1, rowmax, 5
      read (stream, '(6x,I6)', rec=row) val
      print *, val
   end do

   close (stream)
Don't forget to add 1 or 2 characters extra for the record length for the newline. Depends on whether you're on *nix or windows. On windows, add 2, on *nix add 1. In my example, the actual record is only 18 characters long but I have to add 2 extra for the CR, LF.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top