Suggestions on mimicing fgetc and fseek with F90



Hi all,

I know that many compilers have hooks to the C routines fgetc and fseek. I' However, I was hoping to use standard F90 I/O to do this. I read data files that vary from 3MB to 45MB in size. So sometime the whole file is read to access data. Other times I seek into the file to pluck a few bytes out.

Would something like the following be practical

To mimic reading a file in a fgetc fashion

character(1) c1
character(100) text100
open(iunit, fname='foo', form='formatted',access='sequential')

do i =1,100
  read(iunit,'(A1)',advance='no')c1
  text100(i:i) = c1
end do


The to do a seek followed by a read I was thinking of something like the following that would read characters starting at the 300th character and stopping at the 399th character.


character(1) c1
character(100) text100
open(iunit, fname='foo', form='formatted',access='direct',recl=1)

j = 1
do i =300,399
  read(iunit,rec=i)c1
  text100(j:j) = c1
  j = j +1
end do


Does this seem like a reasonable approach? Are there any known speed issues with this approach? Fgetc and fseek are working for me now, but I would like to removed the non-standard fortran calls.


Thanks in advance,

Mike Sutton

.



Relevant Pages

  • Re: Read only last line-
    ... the number of characters written to a stream need not be the ... use state-dependent encodings for extended character sets. ... by implementing fseek() as read-and-count, ... would meet the letter of the law; ...
    (comp.lang.c)
  • Re: How to detect an empty file?
    ... /* We are not at end of buffer ... ... feofis not set until at least one character of I/O is attempted. ... You could -try- to fseek() to the end of the file. ... but on a binary stream the fseekresult can be read off ...
    (comp.lang.c)
  • Re: reading a file in reverse order (bootom-top)
    ... Go to the botton of the file fseek(). ... move one character back to ... feed pair, rather than a single newline character, which is why you ... grab a chunk of characters into a buffer, ...
    (comp.lang.c)
  • Re: jump over lines in C
    ... How to jump over a prespecified number of lines without doing fgets? ... If you know the length of the lines then fseek() can be used. ... Otherwise, you'll have to scan the file, character by character until ...
    (comp.lang.c)