fortran - 在 Fortran 中读取二进制文件
问题描述
我正在尝试读取一个由带符号的 16 位整数组成的二进制文件,其中正好有 51840000 个。完成此操作的代码C
如下所示:
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
int main()
{
int16_t *arr = malloc(51840000*sizeof(int16_t));
FILE *fp;
fp = fopen("LDEM_45N_400M.IMG", "rb");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fread() function: \n\n");
fread(arr, sizeof(*arr), 51840000, fp);
fclose(fp);
printf("%d \n", arr[51840000-2]);
free(arr);
return 0;
}
我将如何在 Fortran 中读取这样的文件?Fortran 中的 IO 对我来说一直很神秘。
解决方案
使用access="stream"
,您可以以相同的方式阅读它。将您的整数声明为integer(int16)
并使用该iso_fortran_env
模块。
use iso_fortran_env
integer :: ierr, n = 51840000
integer(int16) :: arr(n)
open(newunit=iu,file="LDEM_45N_400M.IMG", access="stream", status="old", action="read",iostat=ierr)
if (ierr/=0) stop "Error opening the file."
read(iu, iostat=ierr) arr
if (ierr/=0) stop "Error reading the array."
close(iu)
推荐阅读
- ios - 如何从 NSExpression 中提取 Int 值?
- spring-boot - 使用放心框架测试服务器发送事件
- go - 无法在golang中使用公钥加密
- greenplum - phpadmin4 支持 postgresql 8.3 吗?
- amazon-web-services - 无法使用 Terraform 为 ElasticSearch 添加 2 个子网
- android - 从 android 应用程序中提取私钥和证书
- r - 在 R 中循环数据帧的最佳方法
- python - 如何在python中的子数组上划分数组?
- python - 渲染存储在 Database Flask SQLAlchemy 中的 HTML
- r - 是否有像 MsAccess-display-two-columns-in-combo-box 这样的 R Shiny 组合框