在F90中读取C数据文件
我不是编程专家,但有一些经验。我尝试将数据文件从 C 读入 Fortran 程序已经超过一周了。 C 程序将矩阵保存在 bin 格式数据文件中,如下所示:
FILE * amatFile;
amatFile = fopen("A.dat","wb");
for(krowa=0;krowa<N2;krowa++){
fwrite(amat[krowa], sizeof(float), S2, amatFile);
}
fclose(amatFile);
我在 F90 中的阅读部分是:
open(unit=1,file='AMAT.dat',form='unformatted')
DO i = 1,M
Do j = 1,N
READ(unit=1) AMAT(i,j)
A(i,j) = AMAT(i,j)
End do
End Do
close(1)
如果您能帮助我解决问题,我真的很感激。
I am not an expert in programming but have some experience. It is more than a week that I am trying to read a data file from C into a Fortran program. C program saves a matrix in a bin format data file as follow:
FILE * amatFile;
amatFile = fopen("A.dat","wb");
for(krowa=0;krowa<N2;krowa++){
fwrite(amat[krowa], sizeof(float), S2, amatFile);
}
fclose(amatFile);
and my read section in F90 is:
open(unit=1,file='AMAT.dat',form='unformatted')
DO i = 1,M
Do j = 1,N
READ(unit=1) AMAT(i,j)
A(i,j) = AMAT(i,j)
End do
End Do
close(1)
I really appreciate if you can help me to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于 Fortran IO 的详细记录,我认为您误解了'未格式化”。无格式并不意味着二进制,它只是意味着分隔文本。您的 C 程序肯定没有编写分隔符。如果您可以更改 C 代码,最简单的解决方案是使用 fprintf 而不是 fwrite,并安排格式以符合 Fortran IO 的期望。如果不能,那么我建议编写另一个 C 程序来读取现有程序的输出并写入一些与 Fortran 兼容的数据。
Based on a rather detailed writup of Fortran IO, I think you are misunderstanding 'unformatted'. Unformatted doesn't mean binary, it just means delimited text. Your C program is surely not writing delimiters. The easiest solution, if you can change the C code, is to use fprintf instead of fwrite, and arrange the format to match Fortran IO's expectations. If you can't, then I recommend writing another C program to read the output of the existing one and write some fortran-compatible data.
如果您有选择,请考虑使用 netcdf 或 hdf5。
Fortran io 是主要的痛苦。
http://local.wasp.uwa.edu.au/~pbourke /dataformats/fortran/
但请检查您的输入。
您似乎正在将向量写入文件,但您似乎正在从不同的文件读取矩阵
if you have option, consider using netcdf or hdf5 instead.
fortran io is major pain.
http://local.wasp.uwa.edu.au/~pbourke/dataformats/fortran/
but do check your input.
You seem to be writing vector to file, but you seem to be reading matrix from a different file