get file size

Hi!

The following program fails on PGI 14.7 and 15.7. Intel fortran, NAG fortran and gfortran runs it just fine.

Is this fixed in 15.10? Is there a workaround - that does NOT involve interfacing to various C system calls? Is there a portable workaround at all?

~/testprog % cat foo.f90 
program foo
    implicit none
    integer :: nbytes

    inquire (file='foo.f90', size=nbytes)
    write (*,*) 'File size of this file is:', nbytes
end program foo
~/testprog % wc -c foo.f90; gfortran foo.f90 && ./a.out && pgfortran foo.f90 && ./a.out
164 foo.f90
 File size of this file is:         164
 File size of this file is:           -1

~/testprog % gfortran --version 
GNU Fortran (Ubuntu 4.8.5-1ubuntu1~14.04) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

~/testprog % pgfortran -V   

pgfortran 15.7-0 32-bit target on x86-64 Linux -tp sandybridge
The Portland Group - PGI Compilers and Tools
Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.

If there is a nonstandard PGI routine that we can use, but enable with #ifdef __PGI preprocessing, that would be acceptable. I can’t find any mention of such a nonstandard routine in the PGI docs though. gfortran does support stat, which would be an example of a nonstandard routine that we’d avoid if possible, but that would work if not.

https://gcc.gnu.org/onlinedocs/gfortran/STAT.html

Hi Dynetrekk,

This looks like a compiler error so I’ve added a problem report, TPR #22066, and sent it to engineering for further investigation.

Yes, we do have a lib3f interface for the system’s “STAT” routine. The 3F routines are documented in chapter 9 of the PGI Fortran Reference Guide (http://www.pgroup.com/doc/pgifortref.pdf)

Here’s an example:

% cat stat.f90
          PROGRAM test_stat
            integer, dimension(80) :: buff
            INTEGER :: status
            integer ::STAT

            status = STAT("stat.f90", buff)

            IF (status == 0) THEN
              WRITE (*, FMT="('File size:',               T30, I19)") buff(8)
            END IF
          END PROGRAM

% wc -c stat.f90 ; pgfortran stat.f90 ; a.out
348 stat.f90
File size:                                   348

Thanks for the report!
Mat

Hi Mat,

thanks for the workaround, and for making the bug report. For us, making software that we ship out to be run by external customers, it’s very important to have robust handling of files, environment variables, and so forth. I’d appreciate a bit more attention to detail in these issues.

By the way, the standard INQUIRE statement seems to always return -1.

cheers
Paul

Hi Paul,

FYI, another work around for this is to open the file before inquiring it’s size.

  • Mat

TPR 22066 - UF: Inquire’s size specifier returning bad value

has been fixed in the current 16.1 release.

dave

Won’t that result in a nasty runtime error if the file is larger than 2 GB? We haven’t managed to get large files to work on PGI Fortran 15.7 for some reason.

EDIT: I was talking about the workaround.

Thanks for the fix in the 16.1 release. We’re unable to upgrade though; our IT admin was unable to get the licensing working for anything beyond 15.7 (we try to keep up with new versions). Let us know if that has been fixed, too (at the time PGI support said it was a known bug in the licensing system).