READ Skips First Three Bytes of Files

I’m having problems running the following program (read.for):

PROGRAM READ_ERROR
CHARACTER BUF*1000
INTEGER IO, NSTART, NEND, NCHAR
DIMENSION ISTAT(12)

OPEN(10, FILE=‘./test.run’, FORM=‘UNFORMATTED’, IOSTAT=IO,
1 ACCESS=‘SEQUENTIAL’, ACTION=‘READ’)

IO = FSTAT(10, ISTAT)
NCHAR = ISTAT(8)
NSTART = 1
NEND = NSTART+NCHAR-1

READ(10, IOSTAT=IO) BUF(NSTART:NEND)
CLOSE(10)
STOP
END

on a test.run file containing:
012345678910

and compiled with
pgf95 -g read.for -o read

After the READ statement, IO is -1 and BUF(NSTART:NSTART) is ‘3’. In other words the first three bytes ‘012’ are skipped.

Any ideas why the program would skip the first three bytes of the read?

Thanks,

Brian

I included an error in the previous explanation. The test.run file had a newline before the numbers started. Removing the newline causes buf(1:1) to contain ‘4’, which means that the read is skipping the first 4 bytes.

My apologies,

Brian

Hi Brain,

You need to use formatted I/O since “test.run” is a text file. Unformatted I/O is record based and the first 4 bytes of the line tells the record size.

For example:

% cat read.f90
PROGRAM READ_ERROR
CHARACTER BUF*1000
INTEGER IO, NSTART, NEND, NCHAR
DIMENSION ISTAT(12)

OPEN(10, FILE='./test.run', FORM='FORMATTED', IOSTAT=IO, &
 ACCESS='SEQUENTIAL', ACTION='READ')

IO = FSTAT(10, ISTAT)
NCHAR = ISTAT(8)
NSTART = 1
NEND = NSTART+NCHAR-1

READ(10,IOSTAT=IO,*) BUF(NSTART:NEND)
CLOSE(10)
write(*,*) BUF(NSTART:NEND)
STOP
END

% pgf90 read.f90
% a.out
 012345678910
FORTRAN STOP

Hope this helps,
Mat

When I change the ACCESS to ‘STREAM’ it starts reading at the first byte. I apologize to anyone whose time I’ve taken up with this question. I clearly could have read some documentation from somewhere.