Fortran 77 符合 POSIX 规范吗? - 写入二进制文件

发布于 2024-07-16 17:32:23 字数 1452 浏览 6 评论 0原文

我正在尝试从 Fortran 编写二进制数据文件,但我发现常规文件接口非常有限,我想知道 Fortran 是否具有 POSIX 兼容功能。

我找到了这个标准:IEEE 1003.9-1992,但我不知道如果最常见的编译器完全支持它(或者如果我必须激活任何标志)。 我找不到任何实用的信息,你能给我一些建议吗?

顺便说一句,我正在尝试编写图像文件,首先我想尝试 tga/pgm。 另外我不想添加任何对外部库的依赖,谢谢!

解决方案:

最后我能够编写ppm 文件,我必须避免打印换行符< /a> 在内部循环结束时,最终的代码是这样的:

      subroutine imgwrite()
      implicit none
*     ******************************************************************
      include 'image.f'
*     ******************************************************************
      integer x, y, i

      write(imgunit, '(A)') 'P3'
      write(imgunit, '(I4)') imgwidth
      write(imgunit, '(I4)') imgheight
      write(imgunit, *) 255

      do 10 y=1, imgheight
        do 20 x=1,imgwidth
          write(imgunit,100) (int(imgpixels(x,y,i)*255D0), i=1,3)
20      continue
        write(imgunit,110)
10    continue


100   FORMAT(3(I4),$)
110   FORMAT((/))

      return
      end

没有人提供有关 FORTRAN 中的 POSIX 的任何信息。

谢谢。

I'm trying to write binary data files from fortran, but I find the regular file interfaces very limiting, I wonder if Fortran has POSIX compilant functions.

I found this standard: IEEE 1003.9-1992, but I don't know it if is fully supported by most common compilers (or if I have to activate any flags). I can't find any practical information, can you give me any suggestion?

BTW I'm trying to write image files, first I want to try with tga/pgm. Also I don't want to add any dependency to external libraries, thanks!

Solution:

Finally I was able to write ppm files, and I had to avoid printing newline at the end of the internal loop, the final code is like this:

      subroutine imgwrite()
      implicit none
*     ******************************************************************
      include 'image.f'
*     ******************************************************************
      integer x, y, i

      write(imgunit, '(A)') 'P3'
      write(imgunit, '(I4)') imgwidth
      write(imgunit, '(I4)') imgheight
      write(imgunit, *) 255

      do 10 y=1, imgheight
        do 20 x=1,imgwidth
          write(imgunit,100) (int(imgpixels(x,y,i)*255D0), i=1,3)
20      continue
        write(imgunit,110)
10    continue


100   FORMAT(3(I4),$)
110   FORMAT((/))

      return
      end

Nobody provided any information about POSIX in FORTRAN.

Thanks.

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

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

发布评论

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

评论(2

难理解 2024-07-23 17:32:23

TGA 是一种非常简单的格式,尽管它是二进制的,所以应该可以从 FORTRAN 中编写它。 然而,如果没有外部库的帮助,我不想在 FORTRAN 中尝试 TIFF、PNG、GIF 或 JPG。

您可能会发现 netpbm 格式系列之一更容易编写,并且可以轻松转换为TGA 或您可能需要使用简单命令进行显示或进一步处理的任何其他格式。 PBM 格式包括纯 ASCII 和二进制格式,因此即使二进制文件访问出现问题,也可以写入它们(尽管效率非常低)。 该工具套件包括与大约 100 种其他图像文件格式之间的转换。

编辑:抱歉,对于如何实际使用 FORTRAN 编写文件,我无法提供更多帮助,但当我上次使用该语言时,打孔卡很常见。 ;-)

TGA is a very plain format even though it is binary, so it should be possible to write it from FORTRAN. I wouldn't want to attempt TIFF, PNG, GIF, or JPG in FORTRAN without the help of external libraries, however.

You might find that one of the family of netpbm formats is easier to write, and they would be easily converted to TGA or any other format you might need for display or further processing with simple commands. The PBM formats include both pure ASCII and binary flavors so they can be written (albeit very inefficiently) even if binary file access is problematic. The tool suite includes conversion to and from about 100 other image file formats.

Edit: Sorry that I can't be more helpful about how to actually go about writing files from FORTRAN, but punch cards were common when I last used the language. ;-)

焚却相思 2024-07-23 17:32:23

读写二进制文件(随机或顺序)供其他语言使用的问题是 fortran io 系统使用文件中的其他信息。 我不记得它在 f77 标准中,但肯定大多数 pc 和 unix 编译器都是这样做的。

例如,如果写入二进制顺序文件

write(bsf) doublevariable, realvariable

,则该文件将包含(如果您可以读取二进制)

12<value of double><value of real>12

每个记录都有记录标记,这些标记是在前端和末尾写入的内容的长度(以字节为单位)。
这是必要的,因为每个记录可以有不同的长度,并且在读取时不必读取为记录写入的所有内容。
例如,如果一个人正在读取上面的内容,那么

read(bsf) doublevariable

bsf 的下一次读取需要位于下一条记录的开头。

因此,读取或写入与其他语言共享的二进制文件应该通过用 C 或任何不执行 Fortran 功能的语言编写的库来完成,

Problem with reading and writing binary files, random or sequential, for other languages to use is that the fortran io system uses other information in the file. I cannot recall it being in the f77 standard but certainly most pc and unix compilers did this.

For example if writing to a binary sequential file

write(bsf) doublevariable, realvariable

then the file would contain (if you could read binary)

12<value of double><value of real>12

Each record has record markers which are the length in bytes of what was written at the front and end.
This is needed because each record can have a different length and when reading one does not have to read everything that was written for a record.
For example if one was reading the above one could have

read(bsf) doublevariable

The next read of bsf needs to be at the start of the next record.

So reading or writing binary files to share with other languages should be done via a library written in C or any language that does not do what Fortran does,

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