我如何用C读的Fortran二进制文件? [英] How do I read fortran binary file in C?

查看:369
本文介绍了我如何用C读的Fortran二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有FORTRAN code生成的二进制文件。此文件包含双打的数组。我需要在我的C程序中打开它,然后使用它作为与通常的数组。

I have a binary file generated by fortran code. This file contains an array of doubles. I need to open it in my C program and then work with it as with an usual array.

我该怎么办呢?

如何double类型重新在二进制文件presented?

How double type is represented in binary file?

推荐答案

几乎所有的Fortran编译器,未格式化的(即二进制)顺序文件,写在每个记录的开始和结束时记录的长度。通常情况下,长度是用四个字节,尽管一些编译器使用八个字节。 A记录是与一个单一的Fortran语言写的语句写的一切。如果该阵列与此类型文件的单个Fortran语言写的语句写的,那么你有四个或八个字节跳过,那么阵列,然后四个或八个额外的字节。您可以通过使用十六进制编辑器查看该文件很可能摸不着头脑。如果阵列是写在片(例如,按列或行)具有多个写语句,每个记录将具有这种结构。

Almost all Fortran compilers, for unformatted (i.e., binary) sequential files, write the length of the record at the beginning and end of each record. Typically the length is written in four bytes, though some compilers use eight bytes. A record is everything that is written with a single Fortran write statement. If this array was written with a single Fortran write statement with this type of file, then you have four or eight bytes to skip over, then the array, then four or eight extra bytes. You can probably figure this out by viewing the file with a hex editor. If the array was written in pieces (e.g., by columns or rows) with multiple write statements, each record will have this structure.

如果该文件是由Fortran语言写成未格式化的Fortran语言double类型是写在该计算机的二进制重新presentation。你应该能够在C阅读同一台计算机上的类型。如果要更改架构,而不仅仅是语言的二进制重新presentation可能有所不同。

If the file is written by Fortran as unformatted, the Fortran double type is written in the binary representation of that computer. You should be able to read it in C on the same computer type. If you are changing architectures and not just languages the binary representation might be different.

的Fortran具有其他文件的访问的方法不包括记录长度的信息。直接访问,例如。流访问的新方法是专为跨语言的兼容性。

Fortran has other file access methods which don't include the record length information. Direct access, for example. The new method of stream access is designed for inter-language compatibility.

如果数组是多维的,你可能要调换它,因为Fortran有列主阵列,而C使用行为主。

If the array is multi-dimensional you may want to transpose it because Fortran has column-major arrays while C uses row-major.

相关问题:的Fortran无格式的文件格式和<一个href=\"http://stackoverflow.com/questions/8131204/reading-fortran-unformatted-file-with-python/8131465\">reading与Python FORTRAN未格式化的文件

Related questions: Fortran unformatted file format and reading fortran unformatted file with python

这篇关于我如何用C读的Fortran二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆