search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

Linux C_純C/C++ 確認檔案是否存在和抓取檔案大小 – jashliao部落格

Linux C_純C/C++ 確認檔案是否存在和抓取檔案大小


GITHUB: https://github.com/jash-git/Jash_LinuxC/tree/master/純C確認檔案是否存在和抓取檔案大小


code:

#include    // 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("c:\\123.bat", "r");
        if(pFile == NULL)
        {
                printf("error.\n");
                return 0;
        }

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

        // close the file.
        fclose(pFile);

        return 0;
}



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦