Yjc 发表于 2024-1-18 15:38:35

Fortran程序编译后拖入文件立刻闪退问题

本帖最后由 Yjc 于 2024-1-18 15:53 编辑

各位老师们好,我在学习Fortran程序时遇到一个问题,我想把写在文件里的一个自由格式的矩阵进行格式化后输出在屏幕上,以下是我的源代码GaussFmtMat.f90。在Codeblocks中利用gfortran编译器编译完成后,运行exe程序,一旦向其中拖入mat.txt,程序立刻闪退,在编译器中运行时还有以下提示:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
||Warning: Nonexistent include directory 'D:\Documents\Fortran_Exercise\GaussFmtMat.os_output_dir' [-Wmissing-include-dirs]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

At line 32 of file D:\Documents\Fortran_Exercise\GaussFmtMat.f90 (unit = 10, file = 'D:\Documents\Fortran_Exercise\mat.txt')Fortran runtime error: End of file


Error termination. Backtrace:


Process returned 2 (0x2)   execution time : 10.853 s


请问为什么会导致这个问题?应该怎么解决呢?谢谢各位老师指点!








li913 发表于 2024-1-18 17:29:24

do i=1,ndim,1
        do j=1,ndim,1
                read(10,*) mat(i,j)
        end do
end do
!改为
do i=1,ndim,1
                read(10,*) mat(i,:)
end do

Yjc 发表于 2024-1-18 17:44:09

li913 发表于 2024-1-18 17:29
do i=1,ndim,1
        do j=1,ndim,1
                read(10,*) mat(i,j)


谢谢老师解答,这样在编译器中确实可以运行了,但是如果直接运行编译产生的GaussFmtMat.exe,在输入前面三个信息之后还是会产生闪退问题,这又是什么原因呢?谢谢!

fcode 发表于 2024-1-19 08:13:21

一般是文件路径问题,可以尝试在命令行中运行,可以看到错误提示。http://1shan.w.fcode.cn

Yjc 发表于 2024-1-20 10:55:52

fcode 发表于 2024-1-19 08:13
一般是文件路径问题,可以尝试在命令行中运行,可以看到错误提示。http://1shan.w.fcode.cn
...

谢谢老师解答,问题已经解决了;是因为没在程序末尾加read(*,*),导致程序运行结束直接退出了
页: [1]
查看完整版本: Fortran程序编译后拖入文件立刻闪退问题