[英]Problem when reading Fortran file with CodeBlocks IDE
Recently, i've begun learning Fortran programmation language.最近,我开始学习 Fortran 编程语言。 I am using CodeBlocks IDE with GNU Fortran Compiler.我正在使用带有 GNU Fortran 编译器的 CodeBlocks IDE。
I have a problem in simple code that i found in a Fortran Course online that explains how to read and write from a file.我在 Fortran 在线课程中发现的简单代码中有一个问题,该课程解释了如何从文件中读取和写入。
The program is the following:该程序如下:
program main
implicit none
character (len=14) :: c1,c2,c3
integer :: n
real :: T
open(unit=10,file='titi.txt')
read(10,*) c1,n,c2
read(10,*) c3,T
close(10)
open(unit=20,file='toto.txt')
write(20,*) c1,'il est',n,c2
write(20,*)'la',c3,'est de',T,'degres'
close(20)
end
Where the file 'titi.txt' contains:其中文件“titi.txt”包含:
bonjour 4 heures
temperature 37.2
The error message that appears in the console is the following:控制台中出现的错误消息如下:
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.
Backtrace for this error:
#0 ffffffff
I tried using the flag我尝试使用标志
-g
And than i found using the debugger that the problem is in the first line where 'read' was used然后我使用调试器发现问题出在使用“读取”的第一行
read(10,*) c1,n,c2
I really don't know how to deal with this.我真的不知道如何处理这个。 The code seems pretty simple to me and i have never seen this error message before, so i don't know what does it mean.代码对我来说似乎很简单,我以前从未见过此错误消息,所以我不知道它是什么意思。
Thanks for your answers in advance.提前感谢您的回答。
Thank you all for your responds.谢谢大家的回复。 Actually what caused the problem is that i was using an old compiler.实际上导致问题的原因是我使用的是旧编译器。 So when i downloaded the last version it all worked perfectly without changing any line in the code.因此,当我下载最后一个版本时,它一切正常,无需更改代码中的任何行。
This is not an answer, but it's too much text for a comment.这不是一个答案,但对于评论来说,它的文字太多了。
It's running fine on my computer.它在我的电脑上运行良好。
Can you compile it with你能用它编译吗
gfortran -g -O0 -fbacktrace -Wall -fcheck=all
That way you should get a lot more information.这样你应该得到更多的信息。 Also, you can add some error checking:此外,您可以添加一些错误检查:
Add the following variables:添加以下变量:
integer :: ios
character(len=100) :: iomsg
Then you can add error checking to all io statements like this:然后,您可以向所有 io 语句添加错误检查,如下所示:
read(10,*) c1,n,c2
becomes:变成:
read(10,*,iostat=ios,iomsg=iomsg) c1,n,c2
if (ios /= 0) then
print*, "Error reading c1, n, c2:"
print*, trim(iomsg)
STOP
end if
That can also give you some hints.这也可以给你一些提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.