cpp 获取文件是否存在 获取文件大小

简介: cpp 获取文件是否存在 获取文件大小
inline bool exists_test0 (const std::string& name) {
    std::ifstream f(name.c_str());
    return f.good();
}
inline bool exists_test1 (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }
}
inline bool exists_test2 (const std::string& name) {
    return ( access( name.c_str(), F_OK ) != -1 );
}
inline bool exists_test3 (const std::string& name) {
//最快
    struct stat buffer;
    return (stat (name.c_str(), &buffer) == 0);
}
int fileSize(const char *add){
    std::ifstream mySource;
    mySource.open(add, std::ios_base::binary);
    mySource.seekg(0,std::ios_base::end);
    int size = mySource.tellg();
    mySource.close();
    return size;
}
int get_file_size(std::string filename) // path to file
{
    FILE *p_file = NULL;
    p_file = fopen(filename.c_str(),"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}
long GetFileSize(std::string filename)
{
    struct stat stat_buf;
    int rc = stat(filename.c_str(), &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}
long FdGetFileSize(int fd)
{
    struct stat stat_buf;
    int rc = fstat(fd, &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}
目录
相关文章
|
2月前
|
存储 Python 内存技术
什么是文件?
什么是文件?
20 0
|
2月前
|
Windows
Hasleo EasyUEFI v5.5单文件版
Hasleo EasyUEFI,轻松管理EFI/UEFI启动项 & 管理EFI系统分区 & 修复EFI系统启动问题!EasyUEFI是免费EFI启动管理软件,用于管理EFI/UEFI启动项,包括创建、删除、编辑、清理、备份和还原EFI/UEFI启动项。
31 0
|
2月前
|
存储 内存技术
什么是文件
什么是文件
33 0
|
IDE Linux 开发工具
.editorconfig文件
.editorconfig文件
58 0
|
存储
Fasta和Fastq文件
Fasta和Fastq文件
201 0
|
XML 数据格式