牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

C++的6种获取文件大小的方法

long getFileSize1(const char* strFileName)
{
    FILE * fp = fopen(strFileName, "r");
    fseek(fp, 0L, SEEK_END);
    long size = ftell(fp);
    fclose(fp);
    return size;
}

long getFileSize2(const char* strFileName)
{
    struct _stat info;
    _stat(strFileName, &info);
    long size = info.st_size;
    return size;
}

long getFileSize3(const char* strFileName)
{
    FILE* file = fopen(strFileName, "rb");
    if (file)
    {
        long size = filelength(fileno(file));
        fclose(file);
        return size;
    }
    return 0;
}

ULONGLONG getFileSize4(const char* strFileName)
{
    CFile cfile;
    if (cfile.Open(strFileName, CFile::modeRead))
    {
        ULONGLONG size = cfile.GetLength();
        return size;
    }
    return 0;
}

long getFileSize5(const char* strFileName)
{
    HANDLE handle = ::CreateFile(strFileName, FILE_READ_EA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    if (handle != INVALID_HANDLE_VALUE)
    {
        long size = ::GetFileSize(handle, NULL);
        ::CloseHandle(handle);
        return size;
    }
    return 0;
}

long getFileSize6(const char* strFileName)
{
    std::ifstream in(strFileName);
    if (!in.is_open()) return 0;

    in.seekg(0, std::ios_base::end);
    std::streampos sp = in.tellg();
    return sp;
}

posted on 2016-03-10 00:53 杨粼波 阅读(12685) 评论(3)  编辑 收藏 引用

评论

# re: C++的6种获取文件大小的方法 2016-03-10 10:57 p

只有一种C++ ;p  回复  更多评论   

# re: C++的6种获取文件大小的方法 2016-03-14 17:35 冬瓜

只能算是2种吧!非标准C++的不能算  回复  更多评论   

# re: C++的6种获取文件大小的方法 2016-03-14 23:46 杨粼波

=.=好吧....  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理