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

源码如下:

#include
#include
#include
#include
#define _AFXDLL
using namespace std;

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

    int size = 0;

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

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

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

VS2005:若编译链接不通过,需要修改工程设置:

(1) Configuration Properties -> C/C++ -> Code Generation -> Runtime Library, 选择"Multi-threaded Debug(/MTd)"

(2) Configuration Properties -> Linker -> Input -> Ignore Specific Library, 输入"msvcprtd.lib"

你可能感兴趣的:(C/C++获取文件大小的多种方法)