在C++中获取大文件的大小
Get the size of big files in c++
我有一个大约 4 GB 的大文件,我想获得正确的字节大小,我试过 tellg();
但结果不正确,我尝试了很多函数,但所有他们失败了,这个想法是有一个像这样的功能:
unsigned long long int GetFileSize(std::string path){
//...code
}
如 Jack in the comments, you can use the std::filesystem::file_size 所述。
如果 C++17 不是一个选项,试试这个:
#include <sys/stat.h>
unsigned long long GetFileSize(std::string path){
struct stat64 file_info;
if (stat64(path.c_str(), &file_info) == 0)
return file_info.st_size;
return 0;
}
在这里使用 TDM-GCC 64 位和 8 GB 文件正常工作。
我有一个大约 4 GB 的大文件,我想获得正确的字节大小,我试过 tellg();
但结果不正确,我尝试了很多函数,但所有他们失败了,这个想法是有一个像这样的功能:
unsigned long long int GetFileSize(std::string path){
//...code
}
如 Jack in the comments, you can use the std::filesystem::file_size 所述。
如果 C++17 不是一个选项,试试这个:
#include <sys/stat.h>
unsigned long long GetFileSize(std::string path){
struct stat64 file_info;
if (stat64(path.c_str(), &file_info) == 0)
return file_info.st_size;
return 0;
}
在这里使用 TDM-GCC 64 位和 8 GB 文件正常工作。