Fortran Coder

标题: open打开已有文件出错,segmentation fault, invalid memory reference [打印本页]

作者: mao    时间: 2018-1-2 21:56
标题: open打开已有文件出错,segmentation fault, invalid memory reference
本帖最后由 mao 于 2018-1-2 21:56 编辑

各位好,
一个很基本的open file问题,不知道为什么会出错。请帮忙看一下。谢谢。

环境:Windows WIN 7 , Code::Blocks 17.12,

[Fortran] 纯文本查看 复制代码
program test_file_open
    implicit none
    open(10, file='123.txt',status='old')
    close(10)
end program


出错信息:

我试过如下:
status是new时候,路径下没有对应文件,新建文件正常。 路径下有同名文件,一样错误。
sttus是replace时候,路径下没有对应文件,能够新建文件。路径下有同名文件,一样错误。

现在感觉只要是要打开原路径中原有的文件就会出这个错误。是编译器或者什么需要设置么,还是什么问题。
谢谢

作者: vvt    时间: 2018-1-3 08:24
我用 Code::Blocks 试了一下,没有问题。不会出错。
你想想看,是不是操作上有问题?比如修改了代码但没有重新编译链接,或者编译链接出错,而运行了原来的程序?
作者: mao    时间: 2018-2-3 23:26
vvt 发表于 2018-1-3 08:24
我用 Code::Blocks 试了一下,没有问题。不会出错。
你想想看,是不是操作上有问题?比如修改了代码但没有 ...

谢谢您的回答。操作应该是对的。各种反复都能重复问题。
我提问后,后来想是不是设置或者什么别的有问题,可能我没有看到。所以就跟着彭国伦老师的书从头一直做到文件操作这部分,期间也没有发现有什么注意事项需要的,所以现在还是有同样的问题。
* 原路径没有文件的话一切正常,可写入。
* 原路径只要有同名文件,status是old, new, replace都是同样的错误。
不知道为什么这样? 权限也该没有问题,管理员权限。
谢谢。
作者: vvt    时间: 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。
作者: mao    时间: 2018-2-4 23:21
vvt 发表于 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 ...

谢谢你。这个问题很让人很奔溃,我再看看吧,谢谢
作者: mao    时间: 2018-2-5 00:19
vvt 发表于 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 ...

测试在FTN95(plato)里面一切正常,所以估计 电脑本身软件冲突 和 语句方面没有问题,是不是codeblocks使用前需要什么特别的设置??

我是如下这样的(基本按照长安大学李博士PPT第一章设置的):
(1) codeblocks软件源: http://www.codeblocks.org/downloads/26, 直接下载的 codeblocks-17.12mingw_fortran-setup.exe

(2) 设置: 按照 http://fcode.cn/guide-39-1.html, “Code::blocks + GFortran开发环境设置方法”

没有其余动作。请问: 可能还需要其他设置么?谢谢。
作者: vvt    时间: 2018-2-5 09:55
不需要额外设置。那您就换个编译器吧
作者: mao    时间: 2018-2-5 10:06
vvt 发表于 2018-2-5 09:55
不需要额外设置。那您就换个编译器吧

谢谢

我今天在另外两台电脑上都试了,同样的设置与代码,同样的错误,感觉是个共性问题。肯定是那个地方没弄好。不然不会这样的。只是不是到原因。。。
作者: pasuka    时间: 2018-2-5 10:23
Fortran规范写得很明白,传送门https://docs.oracle.com/cd/E1995 ... j4m0vnaf/index.html
STATUS=sta
The STATUS=sta clause is optional. sta is a character expression. Possible values are: 'OLD', 'NEW', 'UNKNOWN', or 'SCRATCH'.
'OLD'-- The file already exists (nonexistence is an error). For example: STATUS='OLD'.
'NEW' -- The file doesn't exist (existence is an error). If 'FILE=name' is not specified, then a file named 'fort.n' is opened, where n is the specified logical unit.
'UNKNOWN' -- Existence is unknown. This is the default.
'SCRATCH' -- For a file opened with STATUS='SCRATCH', a temporary file with a name of the form tmp.FAAAxnnnnn is opened. Any other STATUS specifier without an associated file name results in opening a file named 'fort.n', where n is the specified logical unit number. By default, a scratch file is deleted when closed or during normal termination. If the program aborts, then the file may not be deleted. To prevent deletion, CLOSE with STATUS='KEEP'.
The FORTRAN 77 Standard prohibits opening a named file as scratch: if OPEN has a FILE=name option, then it cannot have a STATUS='SCRATCH' option. This FORTRAN extends the standard by allowing opening named files as scratch. @ Such files are normally deleted when closed or at normal termination.
TMPDIR: FORTRAN programs normally put scratch files in the current working directory. If the TMPDIR environment variable is set to a writable directory, then the program puts scratch files there. @

Stack Overflow上面的类似问题以及解决建议
https://stackoverflow.com/questi ... and-the-append-data

作者: mao    时间: 2018-2-5 13:35
pasuka 发表于 2018-2-5 10:23
Fortran规范写得很明白,传送门https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnaf/index.html

Sta ...

谢谢。
看过OPEN的说明,我的代码应该是对的吧,现在的问题是:Status=‘OLD’时候, 文件存在会出错。
作者: pasuka    时间: 2018-2-5 13:40
本帖最后由 pasuka 于 2018-2-5 13:42 编辑
mao 发表于 2018-2-5 13:35
谢谢。
看过OPEN的说明,我的代码应该是对的吧,现在的问题是:Status=‘OLD’时候, 文件存在会出错。 ...

文档不是说了吗?'OLD'-- The file already exists (nonexistence is an error). 文件若是不存在则报错
程序代码没有问题,不能推导出程序要打开的文件及其路径也没问题啊?
再说,Stack Overflow上面的帖子已经给出参考代码,为啥不试着跑跑呢?
师傅领进门,修行在自身。


作者: mao    时间: 2018-2-5 13:54
pasuka 发表于 2018-2-5 13:40
文档不是说了吗?'OLD'-- The file already exists (nonexistence is an error). 文件若是不存在则报错
程 ...

谢谢回复,我试过如下代码。
文件不存在时候,新建和写入都正常。再执行一次(此时文件存在,刚新建的)就出错。所以路径应该是没有问题的,并且(io=0)这个文件是正常关掉的,下次打开不应该有问题的么。

[Fortran] 纯文本查看 复制代码
program main
  implicit none
  logical :: exist
  integer(kind=4):: a(20), i, io
  a=(/(i,i=1,20)/)
  inquire(file="te.dat", exist=exist)
  if (exist) then
    write(*,*) exist
    open(10, file="te.dat", status="old")
    read(10, *) a
    close(10)
  else
    write(*,*) exist
    open(10, file="te.dat", status="new")
    write(10, *) a
    close(10,iostat=io)
    write(*,*) io
  end if
  write(*,"(10I3)") a
end


作者: pasuka    时间: 2018-2-5 14:25
mao 发表于 2018-2-5 13:54
谢谢回复,我试过如下代码。
文件不存在时候,新建和写入都正常。再执行一次(此时文件存在,刚新建的) ...

MSYS2 + Gfortran 7.3.0 测试正常
[Fortran] 纯文本查看 复制代码
program main
implicit none
logical:: exist
integer:: flag
character(len=8):: fn="demo.txt"
  
inquire(file=fn, exist=exist)
if (exist) then
    write(*,*)"File already exists."
    open(newunit=flag, file=fn, status="old")
    write(flag, *)"Rewrite file."
    close(flag)
else
    write(*,*)"File does not exist"
    open(newunit=flag, file=fn, status="new")
    write(flag, *)"Create file."
    close(flag)
end if
end program

作者: mao    时间: 2018-2-5 15:16
本帖最后由 mao 于 2018-2-5 15:18 编辑
pasuka 发表于 2018-2-5 14:25
MSYS2 + Gfortran 7.3.0 测试正常
[mw_shl_code=fortran,true]program main
implicit none

谢谢。

我也运行了你的代码,一样的: 第一执行次文件不存在,新建写入正常,第二次执行文件存在,segmentation fault.

这就是目前我遇到的问题:
(1) 文件存在时候,open出错。
(2) 同样的代码大家可以测试通过(环境包括CodeBlocks- by VVT,MSYS2+Gfortran by pasuka)
(3) 我用FTN95+plato也能实现
(4) 但是我在3台不同电脑上用 CodeBlocks+GNU Fortran Complier, 都有问题。

作者: pasuka    时间: 2018-2-5 15:25
本帖最后由 pasuka 于 2018-2-5 15:31 编辑
mao 发表于 2018-2-5 15:16
谢谢。

我也运行了你的代码,一样的: 第一执行次文件不存在,新建写入正常,第二次执行文件存在,segmen ...

gfortran版本呢?CB自带是4.9系列吧?即使5.0版也有反馈出错的
https://sourceforge.net/p/tdm-gcc/bugs/270/




作者: mao    时间: 2018-2-5 15:54
pasuka 发表于 2018-2-5 15:25
gfortran版本呢?CB自带是4.9系列吧?即使5.0版也有反馈出错的
https://sourceforge.net/p/tdm-gcc/bugs/2 ...

谢谢。

现在看起来极有可能是这个编译器版本问题。
弱弱地问下:我不太了解版本,我的版本应该是5.1.0,是么(如下)?我再仔细看下你发的连接,谢谢。


作者: pasuka    时间: 2018-2-5 16:23
mao 发表于 2018-2-5 15:54
谢谢。

现在看起来极有可能是这个编译器版本问题。

命令行下面执行gcc -v或者gfortran -v就能查看版本号

作者: mao    时间: 2018-2-5 18:14
pasuka 发表于 2018-2-5 16:23
命令行下面执行gcc -v或者gfortran -v就能查看版本号

谢谢,我先回去下载个新编译器看看,非常感谢。
作者: mao    时间: 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。

我从MinGW网站下载安装管理器,里面的版本是6.3.0版本,测试可以正常运行。

另请教下 pasuka,为啥我从MinGW下载的版本是6.3.0, 而你那边有更新的7.3.0版本?谢谢,

作者: pasuka    时间: 2018-2-6 14:05
mao 发表于 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。

我从MinG ...

不客气
我不用CB这类IDE做Fortran程序开发,写Makefile也挺好啊
安装新版gcc可以用MSYS2
作者: mao    时间: 2018-2-7 11:39
pasuka 发表于 2018-2-6 14:05
不客气
我不用CB这类IDE做Fortran程序开发,写Makefile也挺好啊
安装新版gcc可以用MSYS2 ...

前天刚装了个MSYS2, 刚接触还不太会用,学习中,有什么好的适合入门的资料么?thanks
作者: pasuka    时间: 2018-2-7 11:48
本帖最后由 pasuka 于 2018-2-7 13:08 编辑
mao 发表于 2018-2-7 11:39
前天刚装了个MSYS2, 刚接触还不太会用,学习中,有什么好的适合入门的资料么?thanks ...
希望活学活用,急用先学,立竿见影的话,首选本站相关教学视频和QQ群 2338021
v.fcode.cn
网上MSYS2的配置教程很多
使用的话,首先得了解Linux基本命令,譬如:
http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html
http://wiki.ubuntu.org.cn/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80



作者: vvt    时间: 2018-2-7 13:12
我用的也是 CodeBlocks + gfortran,大概版本和你的不同。
我也想过会是 gfortran 版本的bug,但是似乎这个bug太明显了,实在不敢相信。
作者: mao    时间: 2018-2-7 14:14
vvt 发表于 2018-2-7 13:12
我用的也是 CodeBlocks + gfortran,大概版本和你的不同。
我也想过会是 gfortran 版本的bug,但是似乎这个 ...

是的啊,这个bug好严重,几乎不能用啊。
还好现在弄清楚了,谢谢。
作者: mao    时间: 2018-2-7 14:16
pasuka 发表于 2018-2-7 11:48
希望活学活用,急用先学,立竿见影的话,首选本站相关教学视频和QQ群 2338021
v.fcode.cn
网上MSYS2的配置 ...

收下大师的八字方针, 谢谢。
从来没接触过linux相关的东西,入门ing
作者: SongAngel    时间: 2018-4-13 17:29
mao 发表于 2018-2-7 14:14
是的啊,这个bug好严重,几乎不能用啊。
还好现在弄清楚了,谢谢。 ...

楼主你好,我今天也遇到这个问题了,我想问下这个编译器是如何进行更新的,第一次使用不是太懂,谢谢了!!!
作者: codeblocker    时间: 2019-3-24 15:04
解决这个问题了吗?如何解决的?我也遇到这个问题了,好像64位系统会出现这个问题,32位系统不会
作者: codeblocker    时间: 2019-3-24 15:15
如果您看到了,麻烦您联系我,非常感谢! 我的邮箱:is1q84@163.com
作者: fcode    时间: 2019-3-24 22:29
codeblocker 发表于 2019-3-24 15:04
解决这个问题了吗?如何解决的?我也遇到这个问题了,好像64位系统会出现这个问题,32位系统不会 ...

把整个帖子看完就知道了
作者: yuchiche    时间: 2019-6-15 14:30
mao 发表于 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。

我从MinG ...

为什么我换成6.0版本的还是会报这样说的错?




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2