爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 8316|回复: 10

[经验总结] 用Fortran获取一个文件的大小

[复制链接]

新浪微博达人勋

发表于 2014-3-26 17:14:00 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 lqouc 于 2014-3-26 18:00 编辑

如何利用Fortran获取文件大小?Fortran里面没有现成的函数可以直接调用。
一种办法是用fortran挨个字节读文件,直到出错停止输出文件多少字节。
但是这样做速度很慢,尤其是遇到大容量文件时机器就卡住了。
我测试过,六百多兆的文件,不知要读多久……
因此,可以考虑采用调用Windows API的办法来解决这个问题,只需要<1秒的时间。

编译需要KERNEL32.MOD和DFWINTY.MOD文件,一般情况下库里已经有了,
在D:\Program Files\Microsoft Visual Studio\DF98\INCLUDE\下可以找到
如果没有那就放到编译的当前目录下编译即可
KERNEL32.MOD (414.88 KB, 下载次数: 2)

评分

参与人数 2金钱 +25 贡献 +7 收起 理由
mofangbao + 15 + 5
lqouc + 10 + 2

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 成长值: 19710
发表于 2014-3-26 21:24:49 | 显示全部楼层
其实用stat函数即可实现:

STAT
Portability Function: Returns detailed information about a file.

Module: USE DFPORT

Syntax

result = STAT (name, statb)


name
(Input) Character*(*). Name of the file to examine.


statb
(Output) INTEGER(4). One-dimensional array with a size of 12.
Results:

The result is of type INTEGER(4). The result is zero if the inquiry was successful; otherwise, the error code ENOENT (the specified file could not be found). For a list of other error codes, see IERRNO.

The elements of statb contain the following values:

Element Description Notes
statb(1)  Device file resides on  Always 0  
statb(2)  File Inode number  Always 0  
statb(3)  Access mode of the file  (See following table)  
statb(4)  Number of hard links  Always 1  
statb(5)  User ID of owner  Always 1  
statb(6)  Group ID of owner  Always 1  
statb(7)  Raw device file resides on  Always 0  
statb(8)  Size of the file in bytes   

statb(9)  Time when the file was last accessed  (Only available on non-FAT file systems; undefined on FAT systems)  
statb(10)  Time when the file was last modified   

statb(11)  Time of last file status change  Same as stat(10)  
statb(12)  Blocksize  Always 1  

Times are in the same format returned by the TIME function (number of seconds since 00:00:00 Greenwich mean time, January 1, 1970).

  1. program file_size
  2. implicit none

  3.     INTEGER*4 ierr, stat, statb(13)


  4.     ierr = stat ( 'rsl.out.0000', statb )
  5.     if ( ierr .ne. 0 ) stop 'stat: error'

  6.     write(*,*)'The size of the file in bytes = ',statb(8)


  7. end program file_size

评分

参与人数 1金钱 +6 收起 理由
lqouc + 6 你为何这么吊!!

查看全部评分

密码修改失败请联系微信:mofangbao
回复 支持 1 反对 0

使用道具 举报

新浪微博达人勋

发表于 2014-3-26 17:58:07 | 显示全部楼层
感谢分享!
ps:稍微改动了一下你的题目,便于搜索。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-3-26 18:08:17 | 显示全部楼层
这个不错 不知道能不能在powerstation里面使用  有空我去试一下
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-3-26 18:23:55 | 显示全部楼层
感谢分享,好方法
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-3-26 20:21:51 | 显示全部楼层
很好啊,希望4.0里面也可以用
密码修改失败请联系微信:mofangbao

新浪微博达人勋

0
早起挑战累计收入
发表于 2014-3-26 20:42:55 | 显示全部楼层
楼主动手能力不错 谢谢分享~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 成长值: 19710
发表于 2014-3-26 21:27:52 | 显示全部楼层
BTW,有的编译器还支持

INQUIRE(FILE=filename, SIZE=file_size)

不过CVF没有size,而stat一般都是有的~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-3-26 23:09:57 | 显示全部楼层
兰溪之水 发表于 2014-3-26 21:24
其实用stat函数即可实现:

STAT

我用的是CVF6.5,INQUIRE(FILE=filename, SIZE=file_size)试过没有成功;
之前在网上也看到过stat函数,但当不知道是在module dfport里面……
我重新试了一下你的方法,好像更简单啊!我今天还帮乎了大半天,就算是学一下怎么调用windows API吧……
program ex
use dfport
implicit none
integer::i,statb(12)
i=stat("D:\a.bin",statb)
print *,Statb(8)
end program
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-3-26 23:50:19 | 显示全部楼层
后来发现原来彭国伦的书上就有!!!
真恨没好好看书啊
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表