backspace error (reading file from the end)
- From: igor <gtg531e@xxxxxxxxxxxxxxxx>
- Date: Mon, 07 Aug 2006 01:18:21 -0400
Hi, everybody,
I have the following question.
I need to read the last several lines
of a very big file with columns of numbers.
(I do not want to read the whole file,
just last several lines)
So, I wrote a subroutine that should do
this for me (see code further).
It reads the last line without a problem.
But when I ask it to read second or third
line from the end, it crashes with error
message
forrtl: severe (23): BACKSPACE error, unit 10, file ...
It should be something obvious, but I do not see what
is wrong. I thought when I use backspace twice I
should be at the position to read the second line
from the end. But instead I get the error.
How do you, people, read files from the end?
Thank you for any suggestions.
program test1
implicit none
integer, parameter :: dp = kind(1.0d0)
character(len=9) :: filename
real(kind=DP) :: line2_i(4)
real(kind=DP) :: line1_i(4)
filename="test.data"
line2_i(1)=tail_read(filename,eline=2,line_size=4,element=1)
line2_i(2)=tail_read(filename,eline=2,line_size=4,element=2)
line2_i(3)=tail_read(filename,eline=2,line_size=4,element=3)
line2_i(4)=tail_read(filename,eline=2,line_size=4,element=4)
print*,'line2=',line2_i
contains
function tail_read(filename,eline,line_size,element) result(arg_out)
implicit none
character(len=*) :: filename
integer, intent(in) :: eline ! number of line from the end
integer, intent(in) :: line_size ! total number of columns
integer, intent(in) :: element ! column to read
real(kind=DP) :: arg_out
integer :: counter
integer :: iocheck
real(kind=DP) :: rarray(line_size)
open(10,file=filename,status='old',mode='read',&
& access='sequential',position='append',iostat=iocheck)
if(iocheck .ne. 0)stop 'STOP! ERROR IN TAIL_READ'
do counter=1,eline
backspace(10)
end do
read(10,fmt=*) rarray
close(10)
arg_out=rarray(element)
end function tail_read
end program test1
--
Igor R.
.
- Follow-Ups:
- Re: backspace error (reading file from the end)
- From: Igor Romanovsky
- Re: backspace error (reading file from the end)
- From: leaf
- Re: backspace error (reading file from the end)
- Prev by Date: Re: can an allocatable array be really dynamic?
- Next by Date: Re: Spacing in derived data types, using SEQUENCE attribute, for MPI
- Previous by thread: Re: Programming Toolshed
- Next by thread: Re: backspace error (reading file from the end)
- Index(es):
Relevant Pages
|