为什么print语句会改变gzread的行为?

 气质沫儿巛1314 发布于 2023-02-04 18:31

我正在尝试使用zlib库中的C函数gzopen,gzread和gzclose在Fortran中读取gzip文件.我的子例程在包含print语句时正常工作,但在Z_STREAM_ERROR (-2)没有它的情况下给出了它.是什么导致这种情况发生,我该如何解决?

module gzmodule
    use :: iso_c_binding
    implicit none
    private
    public fastunzip

    interface
        type(c_ptr) function gzopen(filename,mode) bind(c)
            use :: iso_c_binding
            character(kind=c_char), dimension(*) :: filename
            character(kind=c_char), dimension(*) :: mode
        end function gzopen
    end interface

    interface
        integer(c_int) function gzread(gzfile,buffer,length) bind(c)
            use :: iso_c_binding
            type(c_ptr), value :: gzfile
            character(len=1,kind=c_char) :: buffer(*)
            integer(c_int) :: length
        end function gzread
    end interface

    interface
        integer(c_int) function gzclose(gzfile) bind(c)
            use :: iso_c_binding
            type(c_ptr), value :: gzfile
        end function
    end interface

    contains

        subroutine fastunzip(filename, isize,abuf,ierr)
            use :: iso_c_binding
            character(len=*,kind=c_char), intent(in) :: filename
            integer(c_int), intent(out) :: isize
            character(len=1,kind=c_char), intent(inout) :: abuf(:,:,:,:)
            integer(4), intent(out) :: ierr
            type(c_ptr) :: gzfile
            integer(c_int) :: iclose
            logical :: c_associated
            ierr = 1  !! indicates that an error has occured
            isize = 0
            gzfile = gzopen(trim(filename)//c_null_char,"rb")
            if (.not.c_associated(gzfile)) return
            isize = gzread(gzfile,abuf,size(abuf))
            print*,isize  !! why do I need this for it to work?
            if (isize.ne.size(abuf)) return
            iclose = gzclose(gzfile)
            if (iclose.ne.0) return
            ierr = 0  !! success
        end subroutine fastunzip

end module gzmodule

program main
    use gzmodule        
    implicit none

    character(100) :: filename = './f10_19950120v7.gz'
    integer(4) :: isize
    integer(4) :: ierr
    logical(4) :: exists

    integer(4), parameter :: nlon = 1440
    integer(4), parameter :: nlat = 720
    integer(4), parameter :: nvar = 5
    integer(4), parameter :: nasc = 2
    character(1) :: abuf(nlon,nlat,nvar,nasc)

    inquire(file=filename,exist=exists)
    if (.not.exists) stop 'file not found'

    call fastunzip(filename, isize,abuf,ierr)
    print*,'return value of isize ',isize
    if (ierr.ne.0) stop 'error in fastunzip'

    print*,'done'    
end program main

我在CentOS上并编译:

gfortran -o example_usage.exe example_usage.f90 /lib64/libz.so.1

并且该站点提供了数据文件.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有