long long get_file_size_01(const std::string& filename) {
std::ifstream fin(filename);
fin.seekg(0, fin.end);
auto size = fin.tellg();
fin.close();
return size;
}
long long get_file_size_02(const std::string& filename) {
HANDLE h_file = CreateFileA(filename.data(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
DWORD file_size_hi32;
DWORD file_size_lo32 = GetFileSize(h_file, &file_size_hi32);
if (file_size_lo32 == INVALID_FILE_SIZE)return -1;
CloseHandle(h_file);
long long file_size_hi64 = static_cast<long long>(file_size_hi32);
long long file_size_lo64 = static_cast<long long>(file_size_lo32);
return (file_size_hi64 << 32) | file_size_lo64;
}
long long get_file_size_03(const std::string& filename) {
HANDLE h_file = CreateFileA(filename.data(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
LARGE_INTEGER file_size;
if (GetFileSizeEx(h_file, &file_size) == 0)return -1;
CloseHandle(h_file);
return file_size.QuadPart;
}
이 방법은 윈도우 스토어에서 동작하지 않는다.
long long get_file_size_04(const std::string& filename) {
HANDLE h_file = CreateFileA(filename.data(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
FILE_STANDARD_INFO file_info;
if (GetFileInformationByHandleEx(h_file, FileStandardInfo,&file_info,sizeof(file_info)) == 0)return -1;
CloseHandle(h_file);
return file_info.EndOfFile.QuadPart;
}