Fortran Coder

楼主: mao
打印 上一主题 下一主题

[文件读写] open打开已有文件出错,segmentation fault, invalid memory reference

[复制链接]

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

楼主
发表于 2018-2-5 10:23:53 | 显示全部楼层
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

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

沙发
发表于 2018-2-5 13:40:02 | 显示全部楼层
本帖最后由 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上面的帖子已经给出参考代码,为啥不试着跑跑呢?
师傅领进门,修行在自身。

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

板凳
发表于 2018-2-5 14:25:24 | 显示全部楼层
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

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

地板
发表于 2018-2-5 15:25:16 | 显示全部楼层
本帖最后由 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/



490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

5#
发表于 2018-2-5 16:23:45 | 显示全部楼层
mao 发表于 2018-2-5 15:54
谢谢。

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

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

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

6#
发表于 2018-2-6 14:05:03 | 显示全部楼层
mao 发表于 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。

我从MinG ...

不客气
我不用CB这类IDE做Fortran程序开发,写Makefile也挺好啊
安装新版gcc可以用MSYS2

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

7#
发表于 2018-2-7 11:48:54 | 显示全部楼层
本帖最后由 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


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-5 10:55

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表