- 积分
- 46407
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-9-8
- 最后登录
- 1970-1-1
成长值: 19710
|
发表于 2013-1-29 12:43:33
|
显示全部楼层
本帖最后由 兰溪之水 于 2013-1-29 12:49 编辑
lqouc 发表于 2013-1-29 12:00
没想过要误导谁,就是点个人经验,也提供了较为可行的建议,我写的grd就不行,dat基本都是可以的。扩展名 ...
呵呵,提出质疑很好,首先承认我说得有点不是很对的就是“扩展名没啥意义”,正确的应该说“改变扩展名并不能改变文件的实质”,扩展名的意义是让软件能比较好地识别文件是什么类型的,比如.txt是文本文件,.avi是视频文件,但是如果你把一个文本文件如test.txt改名为test.dat,难道它就变成了二进制文件了么?显然不是,改名后你用记事本依然能正确打开读取,所以扩展名并不完全代表文件的实质,在Linux下更明显了~
好了~现在到用fwrite写出二进制文件的问题了:首先看看fwrite的用法:
---------------------------------------------------------------------------------------------
set fwrite <-be or -le> <-sq or -st> <-ap or -cl> fname
Sets the filename for data output as well as byte ordering and data format.
fname output filename (default = grads.fwrite)
-be output data byte ordering is big endian
-le output data byte ordering is little endian
-sq output data format is sequential
-st output data format is stream (default)
-ap output data is appended to existing file
-cl output data replaces existing file if it exists (default)
---------------------------------------------------------------------------------------------
从上面可以看出,写出二进制文件的一些设置,fname就是文件名,我们可以随便定义,如ps.grd,ps.dat,甚至不要扩展名,直接为ps;还有要注意的问题,关于big endian 和Little endian的问题,默认输出的是little endian,在大部分Unix系统下big endian是比较常见的,所以写程序读取的时候要注意这点;再一个就是sequential和stream的问题,默认是stream存储的格式。上面都是一些需要注意的问题。
所以如果你说读不了,那只可能是你忽略了某些细节。
好吧,既然都花那么多时间码了那么多字了,再给你写个程序吧:
 - program test_grd_dat
- implicit none
- real :: ps_grd(72,46)
- real :: ps_dat(72,46)
- real :: ps(72,46)
- integer :: i,j
- open(10,file='ps.grd',form='unformatted',recl=72*46)
- read(10,rec=1) ((ps_grd(i,j),i=1,72),j=1,46)
- close(10)
- open(20,file='ps.dat',form='unformatted',recl=72*46)
- read(20,rec=1) ((ps_dat(i,j),i=1,72),j=1,46)
- close(20)
- open(20,file='ps',form='unformatted',recl=72*46)
- read(20,rec=1) ((ps(i,j),i=1,72),j=1,46)
- close(20)
- write(*,*) ps_grd(30,40:42)
- write(*,*) ps_dat(30,40:42)
- write(*,*) ps(30,40:42)
- stop
- end
好了,该收拾行李回家了。。。。
@mofangbao大大快给贡献和体力~
|
评分
-
查看全部评分
|