Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我在从外部源读取数据文件时遇到了Fortran的分段错误_Fortran_File Read - Fatal编程技术网

I';我在从外部源读取数据文件时遇到了Fortran的分段错误

I';我在从外部源读取数据文件时遇到了Fortran的分段错误,fortran,file-read,Fortran,File Read,我一直在编写一个程序,使用OPEN语句从文件中读取一个整数值,并在控制台上打印该值 在编译过程中,它看起来是正常的,没有问题,但是当我运行程序时,我遇到了一个分段错误 我已经检查了代码,到目前为止我没有违反任何规则。有人能告诉我这个问题吗 错误: 代码: txt文件的内容只是第一行的数字“1234” Hmmm,代码应该可以工作。如果您也给了我们编译器版本和-选项,那就太好了。 我的意思是,有一件事,你没有关闭你打开的文件,但由于程序终止无论如何,这应该不是一个大问题 我要做的是使用error

我一直在编写一个程序,使用OPEN语句从文件中读取一个整数值,并在控制台上打印该值

在编译过程中,它看起来是正常的,没有问题,但是当我运行程序时,我遇到了一个分段错误

我已经检查了代码,到目前为止我没有违反任何规则。有人能告诉我这个问题吗

错误:

代码:


txt文件的内容只是第一行的数字“1234”

Hmmm,代码应该可以工作。如果您也给了我们编译器版本和-选项,那就太好了。 我的意思是,有一件事,你没有关闭你打开的文件,但由于程序终止无论如何,这应该不是一个大问题

我要做的是使用error status和-message变量,如下所示:

program project5_03
    implicit none
    integer :: n
    integer :: ios
    character(len=200) :: iomsg
    open (unit=21, file='trial.txt', status='old', iostat=ios, iomsg=iomsg)
    call check(ios, iomsg, "OPEN")
    read (21,*, iostat=ios, iomsg=iomsg) n
    call check(ios, iomsg, "READ n")
    write(*,'(1x,a,i4)', iostat=ios, iomsg=iomsg), "this is the value of n", n
    call check(ios, iomsg, "WRITE to STDOUT")
    stop
contains
    subroutine check(ios, iomsg, op)
        implicit none
        integer, intent(in) :: ios
        character(len=*), intent(in) :: iomsg, op
        if (ios == 0) return
        print*, "Encountered Error ", ios, " during ", op
        print*, iomsg
        stop
    end subroutine check
end program
也许这会帮助你找到bug


还有一个事实是,您使用的是
integer::n=0
——这应该不是主程序的问题,但在子例程中,这意味着
保存
属性。

Hmmm,代码应该可以工作。如果您也给了我们编译器版本和-选项,那就太好了。 我的意思是,有一件事,你没有关闭你打开的文件,但由于程序终止无论如何,这应该不是一个大问题

我要做的是使用error status和-message变量,如下所示:

program project5_03
    implicit none
    integer :: n
    integer :: ios
    character(len=200) :: iomsg
    open (unit=21, file='trial.txt', status='old', iostat=ios, iomsg=iomsg)
    call check(ios, iomsg, "OPEN")
    read (21,*, iostat=ios, iomsg=iomsg) n
    call check(ios, iomsg, "READ n")
    write(*,'(1x,a,i4)', iostat=ios, iomsg=iomsg), "this is the value of n", n
    call check(ios, iomsg, "WRITE to STDOUT")
    stop
contains
    subroutine check(ios, iomsg, op)
        implicit none
        integer, intent(in) :: ios
        character(len=*), intent(in) :: iomsg, op
        if (ios == 0) return
        print*, "Encountered Error ", ios, " during ", op
        print*, iomsg
        stop
    end subroutine check
end program
也许这会帮助你找到bug


还有一个事实是,您使用的是
integer::n=0
——这对于主程序来说应该不是问题,但在子例程中,这意味着
SAVE
属性。

显示如何编译代码是非常重要的。回溯非常可疑,如果使用
-g-fbacktrace
或类似工具,回溯会更有用。您是否成功编译并运行了更简单的代码?请在
open
语句中使用
newunit
说明符。硬编码文件标识单元极易出错。演示如何编译代码是非常必要的。回溯非常可疑,如果使用
-g-fbacktrace
或类似工具,回溯会更有用。您是否成功编译并运行了更简单的代码?请在
open
语句中使用
newunit
说明符。硬编码文件识别单元非常容易出错。非常感谢。根据facebook上Fortran专家的建议,我可以通过更改编译器来解决此问题。:)我必须从TDM gfortran转换为基于Cygwin的fortran。。。。非常感谢你。根据facebook上Fortran专家的建议,我可以通过更改编译器来解决此问题。:)我必须从TDM gfortran转换为基于Cygwin的fortran。。。。谢谢