filesize.h
663 Bytes
#ifndef STIM_UTIL_FILESIZE_H
#define STIM_UTIL_FILESIZE_H
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
namespace stim{
static size_t file_size(std::string filename){
#ifdef _WIN32
HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE) return 0;
LARGE_INTEGER size;
if(!GetFileSizeEx(hFile, &size)){
CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
return (size_t)size.QuadPart;
#else
struct stat sb;
stat(filename.c_str(), &sb);
return sb.st_size;
#endif
}
} //end namespace stim
#endif