C/C++获取文件大小的多种方法
源码如下:
#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"
- 上一篇: thinkPHP5 数据库 添加数据
- 下一篇: thinkphp数据库add操作