open打开已有文件出错,segmentation fault, invalid memory reference
本帖最后由 mao 于 2018-1-2 21:56 编辑各位好,
一个很基本的open file问题,不知道为什么会出错。请帮忙看一下。谢谢。
环境:Windows WIN 7 , Code::Blocks 17.12,
program test_file_open
implicit none
open(10, file='123.txt',status='old')
close(10)
end program
出错信息:
我试过如下:
status是new时候,路径下没有对应文件,新建文件正常。 路径下有同名文件,一样错误。
sttus是replace时候,路径下没有对应文件,能够新建文件。路径下有同名文件,一样错误。
现在感觉只要是要打开原路径中原有的文件就会出这个错误。是编译器或者什么需要设置么,还是什么问题。
谢谢
我用 Code::Blocks 试了一下,没有问题。不会出错。
你想想看,是不是操作上有问题?比如修改了代码但没有重新编译链接,或者编译链接出错,而运行了原来的程序? vvt 发表于 2018-1-3 08:24
我用 Code::Blocks 试了一下,没有问题。不会出错。
你想想看,是不是操作上有问题?比如修改了代码但没有 ...
谢谢您的回答。操作应该是对的。各种反复都能重复问题。
我提问后,后来想是不是设置或者什么别的有问题,可能我没有看到。所以就跟着彭国伦老师的书从头一直做到文件操作这部分,期间也没有发现有什么注意事项需要的,所以现在还是有同样的问题。
* 原路径没有文件的话一切正常,可写入。
* 原路径只要有同名文件,status是old, new, replace都是同样的错误。
不知道为什么这样? 权限也该没有问题,管理员权限。
谢谢。 我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 vvt 发表于 2018-2-4 10:47
我帮不了你了,我自己测试过了,没有问题。我不知道在你的计算机上发生了什么。 ...
谢谢你。这个问题很让人很奔溃,我再看看吧,谢谢 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
不需要额外设置。那您就换个编译器吧
谢谢
我今天在另外两台电脑上都试了,同样的设置与代码,同样的错误,感觉是个共性问题。肯定是那个地方没弄好。不然不会这样的。只是不是到原因。。。 Fortran规范写得很明白,传送门https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnaf/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/questions/15526203/single-command-to-open-a-file-or-create-it-and-the-append-data
pasuka 发表于 2018-2-5 10:23
Fortran规范写得很明白,传送门https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnaf/index.html
Sta ...
谢谢。
看过OPEN的说明,我的代码应该是对的吧,现在的问题是:Status=‘OLD’时候, 文件存在会出错。