您的位置:首页 > 运维架构 > Linux

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

2013-06-18 15:58 369 查看

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得到的文件句柄出发,汇总一下方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: