program test_file_open
implicit none
open(10, file='123.txt',status='old')
close(10)
end program
vvt 发表于 2018-1-3 08:24
我用 Code::Blocks 试了一下,没有问题。不会出错。
你想想看,是不是操作上有问题?比如修改了代码但没有 ...
vvt 发表于 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 ...
vvt 发表于 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 ...
vvt 发表于 2018-2-5 09:55
不需要额外设置。那您就换个编译器吧
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. @
pasuka 发表于 2018-2-5 10:23
Fortran规范写得很明白,传送门https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnaf/index.html
Sta ...
mao 发表于 2018-2-5 13:35
谢谢。
看过OPEN的说明,我的代码应该是对的吧,现在的问题是:Status=‘OLD’时候, 文件存在会出错。 ...
pasuka 发表于 2018-2-5 13:40
文档不是说了吗?'OLD'-- The file already exists (nonexistence is an error). 文件若是不存在则报错
程 ...
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
mao 发表于 2018-2-5 13:54
谢谢回复,我试过如下代码。
文件不存在时候,新建和写入都正常。再执行一次(此时文件存在,刚新建的) ...
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
pasuka 发表于 2018-2-5 14:25
MSYS2 + Gfortran 7.3.0 测试正常
[mw_shl_code=fortran,true]program main
implicit none
mao 发表于 2018-2-5 15:16
谢谢。
我也运行了你的代码,一样的: 第一执行次文件不存在,新建写入正常,第二次执行文件存在,segmen ...
pasuka 发表于 2018-2-5 15:25
gfortran版本呢?CB自带是4.9系列吧?即使5.0版也有反馈出错的
https://sourceforge.net/p/tdm-gcc/bugs/2 ...
mao 发表于 2018-2-5 15:54
谢谢。
现在看起来极有可能是这个编译器版本问题。
pasuka 发表于 2018-2-5 16:23
命令行下面执行gcc -v或者gfortran -v就能查看版本号
mao 发表于 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。
我从MinG ...
pasuka 发表于 2018-2-6 14:05
不客气
我不用CB这类IDE做Fortran程序开发,写Makefile也挺好啊
安装新版gcc可以用MSYS2 ...
mao 发表于 2018-2-7 11:39
前天刚装了个MSYS2, 刚接触还不太会用,学习中,有什么好的适合入门的资料么?thanks ...
vvt 发表于 2018-2-7 13:12
我用的也是 CodeBlocks + gfortran,大概版本和你的不同。
我也想过会是 gfortran 版本的bug,但是似乎这个 ...
pasuka 发表于 2018-2-7 11:48
希望活学活用,急用先学,立竿见影的话,首选本站相关教学视频和QQ群 2338021
v.fcode.cn
网上MSYS2的配置 ...
mao 发表于 2018-2-7 14:14
是的啊,这个bug好严重,几乎不能用啊。
还好现在弄清楚了,谢谢。 ...
codeblocker 发表于 2019-3-24 15:04
解决这个问题了吗?如何解决的?我也遇到这个问题了,好像64位系统会出现这个问题,32位系统不会 ...
mao 发表于 2018-2-6 12:54
谢谢 VVT 和 pasuka 的回答。目前问题已经解决了: 是gFortran编译器版本的问题,如pasuka所说。
我从MinG ...
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |