免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2185 | 回复: 7
打印 上一主题 下一主题

如何得知文件的大小 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-21 22:19 |只看该作者 |倒序浏览
各位好。我最近在solaris上用fortran处理文件,处理之前需要知道文件的大小。当然用ls -l
或者 ls-al 可以知道文件的大小,可是我处理的文件有好多,而且用ls 命令得到一些自己不需要的信息,我只需要文件的大小。请问有什么办法直接得到文件的大小?

论坛徽章:
0
2 [报告]
发表于 2007-09-21 22:54 |只看该作者
du -sk *
or
ls -l |awk '{print $9, $5}'
or
ls -l |awk '{print $9,"is using",$5,"bytes"}'

论坛徽章:
0
3 [报告]
发表于 2007-09-21 23:02 |只看该作者

回复 #1 wenhuiwen 的帖子

http://gcc.gnu.org/onlinedocs/gfortran/STAT.html

Next: SUM, Previous: SRAND, Up: Intrinsic Procedures
6.201 STAT — Get file status

Description:
    This function returns information about a file. No permissions are required on the file itself, but execute (search) permission is required on all of the directories in path that lead to the file.

    The elements that are obtained and stored in the array BUFF:

    buff(1)         Device ID
    buff(2)         Inode number
    buff(3)         File mode
    buff(4)         Number of links
    buff(5)         Owner's uid
    buff(6)         Owner's gid
    buff(7)         ID of device containing directory entry for file (0 if not available)
    buff(         File size (bytes)
    buff(9)         Last access time
    buff(10)         Last modification time
    buff(11)         Last file status change time
    buff(12)         Preferred I/O block size (-1 if not available)
    buff(13)         Number of blocks allocated (-1 if not available)

    Not all these elements are relevant on all systems. If an element is not relevant, it is returned as 0.

    This intrinsic is provided in both subroutine and function forms; however, only one form can be used in any given program unit.
Standard:
    GNU extension
Class:
    Subroutine, function
Syntax:
    CALL STAT(FILE,BUFF[,STATUS])
Arguments:

    FILE         The type shall be CHARACTER(*), a valid path within the file system.
    BUFF         The type shall be INTEGER(4), DIMENSION(13).
    STATUS         (Optional) status flag of type INTEGER(4). Returns 0 on success and a system specific error code otherwise.

Example:

              PROGRAM test_stat
                INTEGER, DIMENSION(13) :: buff
                INTEGER :: status
              
                CALL STAT("/etc/passwd", buff, status)
              
                IF (status == 0) THEN
                  WRITE (*, FMT="('Device ID:',               T30, I19)" buff(1)
                  WRITE (*, FMT="('Inode number:',            T30, I19)" buff(2)
                  WRITE (*, FMT="('File mode (octal):',       T30, O19)" buff(3)
                  WRITE (*, FMT="('Number of links:',         T30, I19)" buff(4)
                  WRITE (*, FMT="('Owner''s uid:',            T30, I19)" buff(5)
                  WRITE (*, FMT="('Owner''s gid:',            T30, I19)" buff(6)
                  WRITE (*, FMT="('Device where located:',    T30, I19)" buff(7)
                  WRITE (*, FMT="('File size:',               T30, I19)" buff(
                  WRITE (*, FMT="('Last access time:',        T30, A19)" CTIME(buff(9))
                  WRITE (*, FMT="('Last modification time',   T30, A19)" CTIME(buff(10))
                  WRITE (*, FMT="('Last status change time:', T30, A19)") CTIME(buff(11))
                  WRITE (*, FMT="('Preferred block size:',    T30, I19)") buff(12)
                  WRITE (*, FMT="('No. of blocks allocated:', T30, I19)") buff(13)
                END IF
              END PROGRAM
         


See also:
    To stat an open file: FSTAT, to stat a link: LSTAT

例子通过了在solaris 10 x86 sun studio 12 的f90下编译运行。具体参数可能有所变化,查查Sun Studio 12: Fortran Library Reference 。

[ 本帖最后由 enod 于 2007-9-21 23:06 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-09-22 00:50 |只看该作者

回复 #3 enod 的帖子

谢谢二楼三楼的答复。呵呵,我按照三楼的做了,可是返回值都是0!

论坛徽章:
0
5 [报告]
发表于 2007-09-22 15:19 |只看该作者

回复 #3 enod 的帖子

我的程序是这样的:
      PROGRAM test_stat
      INTEGER*4 buff(13)
      integer stats(13)
      character*8 fname
      integer*4 length
      fname='/data2/filter/p1.dat'
      call stat(fname,stats)
      length = stats(
      END
可是输出结果是:0, 为什么呢?我的文件的大小可是793 字节,为什么呢?

论坛徽章:
0
6 [报告]
发表于 2007-09-22 22:50 |只看该作者

回复 #5 wenhuiwen 的帖子

character*8 fname??
         ~~~
Try: character*512 fname.

Any error messages? Which version/compiler are you using?

论坛徽章:
0
7 [报告]
发表于 2007-09-23 21:27 |只看该作者

回复 #6 enod 的帖子

呵呵,问题解决了!是character*8 fname 出问题了!fname='/data2/filter/p1.dat'早已超过8了!谢谢 enod 骑士的帮助!你是fortran高手!

论坛徽章:
0
8 [报告]
发表于 2007-09-23 23:08 |只看该作者
长见识了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP