Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

direct access file read

71 views
Skip to first unread message

Rudra Banerjee

unread,
May 17, 2013, 11:49:40 AM5/17/13
to
Hello friends,

I am trying to learn to use direct access file.
Say for simplest example, I have a data file(just random example):
0000
0032
1201
1234
4567
7890
2324
2013

Now, I want to read the last half (i.e. line #5-8).

***important question: Is this the situation we use direct access?

Until now (I need to do this frequently), I read this as:
open(12,file='file_so',status='old')
do i=1,n !n=4
read(12,*) !i.e. skip first for line
end do !

do i=1,p ! p=4
read(12,*)arr(i) ! read the last 4 line
write(*,*)arr(i) !
end do !


I tried to write a code to access that #5 to #8 directly as below, which
tells me I have not understood the concept at all.

Program lrn_da
Implicit None
integer :: i,ln,ar(7),k
inquire(iolength=ln)ar
write(*,*)ln

open(12,file='file_so',access='direct',recl=ln)
do i=4,7
read(12,rec=i)(ar(k),k=1,4)
end do
write(*,*)ar
End Program lrn_da

Any help please?

Clive Page

unread,
May 17, 2013, 2:24:29 PM5/17/13
to
On 17/05/2013 16:49, Rudra Banerjee wrote:
> I tried to write a code to access that #5 to #8 directly as below, which
> tells me I have not understood the concept at all.

Fortran direct-access files are not the same as plain text files and you
will find it hard to read files using direct-access reads unless the
file has been created using a set of Fortran direct-access writes.
What you want to do can probably be done more easily using Stream I/O.
This is a Fortran-2003 facility but it is provided by nearly all modern
compilers even if they are called "Fortran-95".

Some time ago I wrote some notes on using Stream I/O which you can find
here:

http://www.star.le.ac.uk/~cgp/streamIO.html

Hope that helps.

--
Clive Page

Louis Krupp

unread,
May 17, 2013, 3:37:39 PM5/17/13
to
On Fri, 17 May 2013 19:24:29 +0100, Clive Page <use...@page2.eu>
wrote:
The OP should keep in mind that if the records are of different
length, there's no way of knowing the position of, say, the 5th record
without reading the first four.

I have to ask: Is this a solution in search of a problem? Would it
be easier just to skip a bunch of records until hitting the first
record of interest?

Louis

Rudra Banerjee

unread,
May 17, 2013, 3:42:29 PM5/17/13
to
On Fri, 2013-05-17 at 13:37 -0600, Louis Krupp wrote:
> Is this a solution in search of a problem? Would it
> be easier just to skip a bunch of records until hitting the first
> record of interest?
Yes...this is a type I often encounter with spin_up and spin_down
potentials.

Louis Krupp

unread,
May 17, 2013, 10:48:47 PM5/17/13
to
If the files are really, really big (for some defintion of "really
big"), then a direct access file might make sense. If each record
consists of one integer, you should be able to create a direct access
(or possibly stream) file with binary integers instead of ASCII text.

If your performance is acceptable the way things are, then direct
access files are a solution to a problem you don't have, and you might
not want to bother with them.

Louis
Message has been deleted

Robin Vowels

unread,
May 18, 2013, 6:50:00 AM5/18/13
to
You need first to write the data to the direct access file;
only then you can read it the way that you want.

As well, you need to read one item at a time:
read(12,rec=i) ar(i)

e p chandler

unread,
May 18, 2013, 5:31:12 PM5/18/13
to
You can use direct access to read a text file, but I don't think you will be happy with the constraints. All records have to be of the same length. You have to specify formatted I/O and you need an explicit format. You need to take into account any end of line markers and whether these are 1 or 2 characters long.

Unless performance is actually a problem, I don't think it is worth the trouble.
To demonstrate how you might do it, and also why you probably will not want to, see the following:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\epc\temp>type x0.f90
implicit none
integer i

open(10,file='x.txt',form='formatted',status='unknown')
do i=1,10
write(10,'(i3)') i
end do
end

C:\Users\epc\temp>g95 x0.f90
C:\Users\epc\temp>a
C:\Users\epc\temp>type x.txt
1
2
3
4
5
6
7
8
9
10

C:\Users\epc\temp>type x1.f90
implicit none
integer i,j

open(10,file='x.txt',access='direct',form='formatted',recl=5) !cr/lf
do i=1,10
read(10,'(i3)',rec=i) j
print *,j
end do
end

C:\Users\epc\temp>g95 x1.f90
C:\Users\epc\temp>a
1
2
3
4
5
6
7
8
9
10

C:\Users\epc\temp>d x.txt
C:\Users\epc\temp>od -Ax -tx1 -v x.txt
000000 20 20 31 0d 0a 20 20 32 0d 0a 20 20 33 0d 0a 20
000010 20 34 0d 0a 20 20 35 0d 0a 20 20 36 0d 0a 20 20
000020 37 0d 0a 20 20 38 0d 0a 20 20 39 0d 0a 20 31 30
000030 0d 0a
000032

C:\Users\epc\temp>

HTH
---- e

Robin Vowels

unread,
May 23, 2013, 8:07:03 PM5/23/13
to
This doesn't seem to be portable.
5 for LRECL is incorrect for Silverfrost F95.
0 new messages