C/C++获取文件大小的多种方法
2009-04-15 14:47
405 查看
源码如下:
#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"
#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"
相关文章推荐
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小(转)
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小(转)
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- (转)C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- C/C++多种方法获取文件大小
- C++获取文件大小方法
- C和C++获取文件大小的方法总结
- c/c++获取文件大小的方法
- C和C++获取文件大小的方法总结(转)
- VC C/C++ 4种方法获取文件大小 Windows API
- C/C++获取文件大小(长度)的方法
- c/c++获取文件大小的方法
- VC C/C++ 4种方法获取文件大小 Windows API