C + + multiple methods to get file size
#include <iostream>
#include <io.h>
#include <sys\stat.h>
#include <afx.h
#define _afxdll
using namespace std;
Void Main ()
{
//This file is inaccessible under Project open
char* filepath = ". \\TEST.NCB ";
//Method one
struct _stat info
_stat (filepath, &info);
int size = info.st_size;
COUT<<SIZE<&L T;endl;
//Method Two
file* FILE = fopen (filepath, "RB");
if (file)
{
int size = filelength (Fileno (file));
cout& lt;<size<<endl;
fclose (file);
}
//Method three
CFile CFile
if (CFile. Open (filepath, cfile::moderead))
{
int size = CFile. GetLength ();
cout<<size<<endl;
}
//Method four
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);
}
}
You can also call the WINAPI function directly getfilesize
DWORD WINAPI GetFileSize(
__in HANDLE hFile,
__out LPDWORD lpFileSizeHigh
);
Support for Win2000 and future systems
This article supporting source code