C++獲取文件大小常用技巧分享
作者:佚名
我們在這里為大家總結(jié)了四種方法來為大家詳細(xì)講解C++獲取文件大小的實(shí)現(xiàn)。希望初學(xué)者可以以此為參考,來選擇一種適合自己的方法方便實(shí)際應(yīng)用。
C++編程語言在程序開發(fā)應(yīng)用中能夠幫助我們輕松的完成許多功能需求。比如今天為大家介紹的C++獲取文件大小的方法,就可以以多種方式輕松的實(shí)現(xiàn)?,F(xiàn)在將會(huì)實(shí)現(xiàn)方法呈現(xiàn)給大家,以便大家參考。
C++獲取文件大小代碼示例:
- #include < iostream>
- #include < io.h>
- #include < sys\stat.h>
- #include < afx.h>
- #define _AFXDLL
- using namespace std;
- void main()
- {
- // 此文件在工程打開狀態(tài)下為不可訪問
- 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++獲取文件大小的相關(guān)方法就為大家介紹到這里。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園