Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

get/setfilepointer

dave_frank
Beginner
1,763 Views
I would like to rewrite recent data written to a
form='binary' file.

While fseek allows repositioning from current position there is a catch 22, as I have no way of making notations where I am in the variable size records being written to reposition WAAAY-back in the file.

C functions fgetpos() fsetpos() apparently would allow me to do that I want. The question is what win32 interface has the info about file position pointers?
0 Kudos
6 Replies
pcurtis
Beginner
1,763 Views
The Win32 file i/o routines are vastly superior to the
antiquated Fortran approach, in that they provide total
flexibility to write/read any number of bytes to/from any
location in memory, and the file pointer can be
positioned at an arbitrary position within the file prior
to each operation. Email me (pcurtis@kiltel.com) for a
complete module with Win32 F90 file functions.


SUBROUTINE rw_file (rwmode, ihandl, nbytes,loc_pointer, offset)
    IMPLICIT NONE
    CHARACTER(LEN=1), INTENT(IN) :: rwmode
    INTEGER, INTENT(IN)	 :: ihandl, nbytes, loc_pointer
    INTEGER, INTENT(IN), OPTIONAL :: offset
    INTEGER :: nact

    ! position pointer if offset is provided
    IF (PRESENT(offset))  &
      nact = SetFilePointer(ihandl,offset,NULL,FILE_BEGIN)

    IF (rwmode == 'R') THEN
      IF (.NOT.ReadFile (ihandl,     &  ! file handle
                         loc_pointer,&  ! address of data
                         nbytes, &  ! byte count to read
                         LOC(nact), & ! actual bytes read
     		         NULL_OVERLAPPED))	THEN
        CALL API_error ('Error Reading File')
      END IF
		
    ELSE
      IF (.NOT.WriteFile(ihandl, &  ! file handle
			 loc_pointer,& ! address of data
                         nbytes, &  ! byte count to write
			 LOC(nact),& !actual bytes written
                         NULL_OVERLAPPED)) THEN
        CALL API_error ('Error Writing file')
       END IF
    END IF
END SUBROUTINE rw_file
0 Kudos
Steven_L_Intel1
Employee
1,763 Views
The CVF Portability Library (USE DFPORT) routine FSEEK allows you to reposition relative to the beginning or ending of the file, as well as the current position. You can open the file using FORM='BINARY' and write arbitrary amounts of data.

This is not to say that a Win32 API approach might not be better, but you CAN do this using CVF extended Fortran.

Steve
0 Kudos
dave_frank
Beginner
1,763 Views
Paul,
My poorly worded question was almost answered by you.
If you have duplicated the C routines fgetpos, fsetpos
pls send me a copy, OR the module that would allow your subroutine to be compiled (defines file pointer
api interfacing).. to dave_frank@hotmail.com
and I will try to generate a fortran version of
fgetpos fsetpos.

Steve,
I did say fseek did not provide the support I wanted as I may be well past the point that I want to start re-writing the file. Having made a notation of this point
with a fgetpos, would simplify the operation envisaged.
0 Kudos
Steven_L_Intel1
Employee
1,763 Views
Dave,

Perhaps I misunderstand what you want, but it looks as if you didn't read my reply carefully. FSEEK will let you reposition relative to the start of the file if you want. If you want to know where you are currently, use FTELL.

Steve
0 Kudos
pcurtis
Beginner
1,763 Views
The attached file comprises a module containing a set of F90 file-access routines which use Win32 API primitives to get the job done. Although this module compiles and can be USEd as-is, users will need to add their own code for extended error-reporting output (MessageBox or whatever you want) at the indicated place in API_Error.
0 Kudos
dave_frank
Beginner
1,763 Views
Thanks Paul for your code..

The missing piece (of which I was not aware existed)
is FTELL which allows use of LUNIT. The problems associated with file handles are a major dis-connect for me and as far as I can see, FTELL is the same functionally as C's FGETPOS (wonder why the name was changed)...

Thanks Steve, for the FTELL alert..

0 Kudos
Reply