正文

C/C++多种方法获取文件大小2012-10-15 15:19:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yuqiexing/53838.html

分享到:

#include <iostream>
#include <io.h>
#include <sys\stat.h>
#include <afx.h>
#define _AFXDLL
using namespace std;


void main()
{
    // 此文件在工程打开状态下为不可访问
    char* filepath = "..\\test.ncb";


    // 方法一
    struct _stat info;
    _stat(filepath, &info);
    int size = info.st_size;
    cout<<size<<endl;


    // 方法二
    FILE* file = fopen(filepath, "rb");
    if (file)
    {
        int size = filelength(fileno(file));
        cout<<size<<endl;
        fclose(file);
    }


    // 方法三
    CFile cfile;
    if (cfile.Open(filepath, CFile::modeRead))
    {
        int size = cfile.GetLength();
        cout<<size<<endl;
    }


    // 方法四
    HANDLE handle = CreateFile(filepath, FILE_READ_EA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    if (handle != INVALID_HANDLE_VALUE)
    {
        int size = GetFileSize(handle, NULL);
        cout<<size<<endl;
        CloseHandle(handle);
    }
}

阅读(2243) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册