Silverfrost Forums

Welcome to our forums

SIZE keyword in INQUIRE

9 Nov 2025 7:11 #32448

As per code below, Ftn95doesn't support the SIZE keyword in INQUIRE statements. This is quite important I think.

PROGRAM TEST_INQUIRE_SIZE
    IMPLICIT NONE
    INTEGER :: fsize, ios
    LOGICAL :: fexists

    INQUIRE(FILE='test_inquire_size.f90', EXIST=fexists, SIZE=fsize, IOSTAT=ios)

    WRITE(*,*) 'File exists:', fexists
    WRITE(*,*) 'File size:', fsize
    WRITE(*,*) 'IOSTAT:', ios

END PROGRAM
9 Nov 2025 7:12 #32449

Also FILE_SIZE@ and FILE_SIZE8@ seem to fail with files that are already open , so they cannot be used unless we close the file in question file first.

9 Nov 2025 7:14 #32450

It fails also when accessing via unit number INQUIRE(UNIT=iunit, EXIST=fexists, SIZE=fsize, IOSTAT=ios)

9 Nov 2025 11:32 #32451

The SIZE= specifier was added to the INQUIRE statement in Fortran 2003, i.e. after the Fortran 95 standard.

10 Nov 2025 7:23 #32452

Thank you for the feedback. I have made a note that these issues need investigating.

10 Nov 2025 7:30 #32453

StamK

FILE_SIZE@ works for me when the file is open in the editor. Can you provide sample code with the context where it does not work.

10 Nov 2025 10:09 #32454

An alternative is to use fileinfo@ which works as shown below.

program test_inquire_size
    implicit none
    character(260) :: path
    integer(2) :: mode, dev, rdev, nlink, err
    integer(3) :: size, atime, mtime, ctime
    logical :: fexists
    
    path = 'freeformat1.f95'

    inquire(file=path, exist=fexists)
    if (.not. fexists) then
        print *, 'File does not exist.'
        stop
    end if

    call fileinfo@(trim(path)//char(0), mode, dev, rdev, nlink, size, atime, mtime, ctime, err)

    if (err .eq. 0) then
        print *, 'File exists:', fexists
        print *, 'File size (bytes):', size
        print *, 'Access time:', atime
        print *, 'Modified time:', mtime
        print *, 'Creation time:', ctime
    else
        print *, 'Error reading file info. Error code:', err
    end if
end program



 File exists:  T
 File size (bytes):         800
 Access time:  1762769130
 Modified time:  1762769130
 Creation time:  1760533762

C:\Arcadis 2025\0008
10 Nov 2025 2:48 #32455

Quoted from PaulLaidler StamK

FILE_SIZE@ works for me when the file is open in the editor. Can you provide sample code with the context where it does not work.

StamK probably means open in the fopen sense. It doesn't seem unreasonable it wouldn't work especially if the file was open for writing.

10 Nov 2025 8:10 #32456

Yes, I meant in the fopen sense. Regarding using fileinfo@ it would be possible in theory, but haven't tested if it fails for open files.

11 Nov 2025 1:47 #32458

Quoted from StamK It fails also when accessing via unit number INQUIRE(UNIT=iunit, EXIST=fexists, SIZE=fsize, IOSTAT=ios)

My understanding is that INQUIRE ( ...,SIZE=... ) was included in Fortran 03 to support stream access. Perhaps this support could be included as FTN95 now supports stream access, including files/records larger than 2GBytes. Stream access is a very useful file access approach which is very portable between compilers, unlike Fortran unformatted file types.

Using stream access, you can also generate your own record headers/footers to replicate your preferred or other Fortran compiler's unformatted record structures. But be warned, with unformatted records larger than 2 GBytes, the record structures of other compilers become very messy.

19 Nov 2025 11:19 #32490

INQUIRE(SIZE=fsize....) has now been added for the next release of FTN95 and its library. It has been tested for INQUIRE by UNIT number and by FILE name.

FILE_SIZE@ can be expected to fail if the file is already open via Fortran OPEN or via OPENR@ etc.

Please login to reply.