C/C++多种方法获取文件大小
- #include
- #include
- #include
- #include
- #define _AFXDLL
- using namespace std;
- void main()
- {
- // 此文件在工程打开状态下为不可访问
- char* filepath = "..\test.ncb";
- // 方法一
- struct _stat info;
- _stat(filepath, &info);
- int size = info.st_size;
- cout<
- // 方法二
- FILE* file = fopen(filepath, "rb");
- if (file)
- {
- int size = filelength(fileno(file));
- cout<
- fclose(file);
- }
- // 方法三
- CFile cfile;
- if (cfile.Open(filepath, CFile::modeRead))
- {
- int size = cfile.GetLength();
- cout<
- }
- // 方法四
- HANDLE handle = CreateFile(filepath, FILE_READ_EA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
- if (handle != INVALID_HANDLE_VALUE)
- {
- int size = GetFileSize(handle, NULL);
- cout<
- CloseHandle(handle);
- }
- }
也可以直接调用WinAPI函数GetFileSize嘛,
- DWORD WINAPI GetFileSize(
- __in HANDLE hFile,
- __out LPDWORD lpFileSizeHigh
- );
支持Win2000及以后的系统
分享名称:C/C++使用多种方法获取文件大小代码
网站路径:http://www.shufengxianlan.com/qtweb/news31/393781.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联