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

转帖|其它|编辑:郝浩|2009-02-24 14:30:47.000|阅读 1876 次

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

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

源码如下:

#include <iostream>
#include <io.h>
#include <sys\stat.h>
#include <afx.h>
#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<<size<<endl;

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

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

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

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

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


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP