C++获取文件大小的几种方法
2015-11-23 17:53
471 查看
#include <iostream> #include <fstream> #include <windows.h> #include <io.h> #include <sys\stat.h> using namespace std; int main() { char *filepath = "D:\\1.TXT"; //方法一 HANDLE handle = CreateFileA( filepath, FILE_READ_EA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0 ); if ( handle != INVALID_HANDLE_VALUE ) { int size = GetFileSize( handle, NULL ); cout << size << endl; CloseHandle( handle ); } //方法二 C++流获取 ifstream fin( filepath ); if( fin.is_open() ) { fin.seekg( 0, ios::end ); int size = fin.tellg(); fin.close(); cout << size << endl; } //方法三 C标准获取 FILE* file = fopen( filepath, "r" ); if ( file ) { int size = filelength( fileno(file) ); cout << size << endl; fclose( file ); } //方法四 struct _stat info; _stat(filepath, &info); int size = info.st_size; cout << size << endl; return 0; }
相关文章推荐
- C语言的空格问题
- C++中重载、重写(覆盖)和隐藏的区别
- 整数变换问题-----回溯法
- C++将HBITMAP保存为bmp图片
- c++中的一些概念
- BinaryHeap 的C++实现
- BinaryHeap 的C++实现
- BinaryHeap 的C++实现
- BinaryHeap 的C++实现
- C++和JNI的数据转换
- Floyd算法验证-每队顶点之间的最短路径
- C++ DirectX 游戏开发视频教程 08 资源下载链接
- c++类内存结构
- c++实现写日志功能
- 在C++中子类继承和调用父类的构造函数方法
- C++ 获取文件夹下的所有文件名
- 浮点数强制转换为整数高效替代方法
- 12到C++面试题(有趣)
- C++学习归纳与总结
- elipse 报出An internal error occurred during: "C/C++ Indexer". java.lang.NullPointerExcption 错误