欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++获取文件大小的4种常见技巧分享

程序员文章站 2022-06-15 21:17:51
c++编程语言在程序开发应用中能够帮助我们轻松的完成许多功能需求。比如今天为大家介绍的c++获取文件大小的方法,就可以以多种方式轻松的实现。现在将会实现方法呈现给大家,以便大家参考。c++获取文件大小...

c++编程语言在程序开发应用中能够帮助我们轻松的完成许多功能需求。比如今天为大家介绍的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"; 
// c++获取文件大小方法一 
struct _stat info; 
_stat(filepath, &info); 
int size = info.st_size; 
cout< < size< < endl; 
// c++获取文件大小方法二 
file* file = fopen(filepath, "rb"); 
if (file) 
{ 
int size = filelength(fileno(file)); 
cout< < size< < endl; 
fclose(file); 
} 
// c++获取文件大小方法三 
cfile cfile; 
if (cfile.open(filepath, cfile::moderead)) 
{ 
int size = cfile.getlength(); 
cout< < size< < endl; 
} 
// c++获取文件大小方法四 
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< < size< < endl; 
closehandle(handle); 
} 
} 

到此这篇关于c++获取文件大小的4种常见技巧分享的文章就介绍到这了,更多相关c++获取文件大小内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!