将 Fortran 中带有 Err 说明符的 OPEN 函数转换为 PHP 中的 fopen 函数

发布于 2025-01-05 08:43:38 字数 429 浏览 0 评论 0原文

我需要在 php 中找到 fortran 行的等效表达式: fortran 代码:

10    OPEN(UNIT=10,FILE='TEST.OUT',STATUS='NEW', ERR=9001)
      GOTO 11
9001  OPEN(UNIT=10,FILE='TEST.OUT')
      CLOSE(10,STATUS='DELETE')
      OPEN(UNIT=2,FILE='fort2')
      CLOSE(2,STATUS='DELETE')
      GOTO 10
11    OPEN(UNIT=7,FILE='TEST.SUM',STATUS='NEW')

我想将此代码转换为 php 代码。我想使用 fopen 函数,但我不知道如何让它跳转到第 9001 行以首先删除旧文件(如果有任何旧文件)。 非常感谢您的帮助。

I need to find an equivalent expression for a fortran line in php:
fortran code:

10    OPEN(UNIT=10,FILE='TEST.OUT',STATUS='NEW', ERR=9001)
      GOTO 11
9001  OPEN(UNIT=10,FILE='TEST.OUT')
      CLOSE(10,STATUS='DELETE')
      OPEN(UNIT=2,FILE='fort2')
      CLOSE(2,STATUS='DELETE')
      GOTO 10
11    OPEN(UNIT=7,FILE='TEST.SUM',STATUS='NEW')

I would like to convert this code into a php code. I would like to use fopen function but I do not know how to aks it to jump to line 9001 to delete old files fisrt if there is any old file.
Really appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-01-12 08:43:38

我不知道 Fortran 代码到底在做什么,但听起来你想要这样:

$fh = fopen('TEST.OUT', 'w');

注意 'w' 模式的描述:

仅供写入;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。

http://php.net/manual/en/function.fopen.php

否则可能:

do {
    $fh = @fopen('TEST.OUT', 'x');
    if (!$fh) {
        unlink('TEST.OUT');
        unlink('fort2');
    }
} while (!$fh);

'x':创建并打开仅供写入;将文件指针放在文件的开头。如果文件已存在,则 fopen() 调用将失败并返回 FALSE 并生成 E_WARNING 级别的错误。如果该文件不存在,请尝试创建它。

不过,您应该小心不要陷入无限循环。

您也可以自己进行这些检查:

if (file_exists('TEST.OUT')) {
    unlink('TEST.OUT');
    unlink('fort2');
}

I don't know what that Fortran code is doing exactly, but it sounds like you want this:

$fh = fopen('TEST.OUT', 'w');

Note the description of the 'w' mode:

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

http://php.net/manual/en/function.fopen.php

Otherwise possibly:

do {
    $fh = @fopen('TEST.OUT', 'x');
    if (!$fh) {
        unlink('TEST.OUT');
        unlink('fort2');
    }
} while (!$fh);

'x': Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.

You should be careful not to get into an infinite loop there though.

You can also do those checks yourself:

if (file_exists('TEST.OUT')) {
    unlink('TEST.OUT');
    unlink('fort2');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文