Linux下获取文件大小方法汇总

Linux获取文件大小汇总

最近纠结一个问题,Linux下如何获取文件的大小,查了一下,方法汇总如下:
1.利用fseek和ftell
2.利用fstat和fileno
下面我们就详细说一下介绍一下。

1.利用fseek和ftell获取文件的大小

1.1fseek、ftell介绍

int fseek(FILE *stream, long offset, int whence);
功能:该函数是将文件指针移到指定的位置。
参数:stream表示用fopen打开的文件HANDLE(入参)
            offset表示文件指针的偏移位置(入参)
            whence表示从何处偏移(入参)。whence有三种选项,分别代表:
                                                                          SEEK_SET:从文件开头偏移offset个byte;
                                                                          SEEK_CUR:从文件当前位置偏移offset个byte;
                                                                          SEEK_END:从文件末尾偏移offset个byte;
返回值:成功为0,失败为-1;

long ftell(FILE *stream);
功能:获取文件指针从文件开始到当前位置的偏移量。
参数:stream表示用fopen打开的文件HANDLE(入参)
返回值:成功就返回偏移量;失败为-1.

1.2如何获取文件大小?

主要思路是这样的:首先利用fseek将文件指针指向文件末尾,然后利用ftell获取文件的当前位置。

1.3测试程序

#include <stdio.h>   // FILE...

// get the file size.
long getfilesize(FILE *pFile)
{
        // check FILE*.
        if( pFile == NULL)
        {
                return -1;
        }

        // get current file pointer offset.
        long cur_pos = ftell(pFile);
        if(cur_pos == -1)
        {
                return -1;
        }

        // fseek to the position of file end.
        if(fseek(pFile, 0L, SEEK_END) == -1)
        {
                return -1;
        }

        // get file size.
        long size = ftell(pFile);

        // fseek back to previous pos.
        if(fseek(pFile, cur_pos, SEEK_SET) == -1)
        {
                return -1;
        }

        // deal returns.
        return size;
}
int main()
{
        // open a file.
        FILE *pFile = fopen("./Test.txt", "a");
        if(pFile == NULL)
        {
                printf("error.\n");
                return 0;
        }

        // get the file size.
        printf("the file size: %ld\n", getfilesize(pFile));

        // close the file.
        fclose(pFile);

        return 0;
}

2.利用fstat和fileno

2.1fileno和fstat介绍

int fileno(FILE *stream);
功能:将stream转化为整数描述符。
参数:stream表示用fopen打开的文件HANDLE(入参)
返回值:成功返回stream的整数描述符;失败返回-1.

int fstat(int filedes, struct stat *buf);
功能:通过文件描述符filedes获取该文件信息
参数:filedes表示文件描述符(入参)
            buf表示文件信息结构的指针(出参)。struct stat的具体信息可以查看(http://www.cnitblog.com/guopingleee/archive/2008/11/13/51411.aspx)
返回值:成功返回0;失败返回-1.

2.2如何获取文件大小?

首先利用fileno将“FILE *”型的HANDLE转化为文件的整数描述符,然后获取文件的信息(其中包含文件大小)。

2.3测试程序

#include <sys/stat.h>// stat
#include <stdio.h>   // FILE...

// get the file size.
off_t getfilesize(FILE *pFile)
{
        // check FILE*.
        if( pFile == NULL)
        {
                return -1;
        }

        // get file size.
        int fd=fileno((FILE *)pFile);
        if(fd == -1)
        {
                return -1;
        }
        struct stat fileStat;
        if( -1 == fstat(fd, &fileStat))
        {
                return -1;
        }

        // deal returns.
        return fileStat.st_size;
}
int main()
{
        // open a file.
        FILE *pFile = fopen("./Test.txt", "a");
        if(pFile == NULL)
        {
                printf("error.\n");
                return 0;
        }

        // get the file size.
        printf("the file size: %ld\n", getfilesize(pFile));

        // close the file.
        fclose(pFile);

        return 0;
}

后记:
获取文件大小的方法还有很多,本文主要是从fopen得到的文件句柄出发,汇总一下方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值