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;
}
目录
相关文章
|
5月前
文件
文件操作。
17 0
|
8月前
|
NoSQL Python
PythonExcel文件
在Python中,我们可以使用许多库来处理Excel文件,其中最常用的是pandas和openpyxl。
|
存储 C语言
文件(下)——“C”
文件(下)——“C”
vb-复制ie临时文件夹下所有mp3文件到指定目录,并且将utf8编码转换过来
vb-复制ie临时文件夹下所有mp3文件到指定目录,并且将utf8编码转换过来
92 0
|
Go 数据安全/隐私保护 Windows
WinNTSetup V5.3.0 Bata5 单文件版
WinNTSetup 是一款Windows系统硬盘安装器,支持从PE和本地安装系统,支持支持NT内核的系统。
WinNTSetup V5.3.0 Bata5 单文件版
|
C#
C#生成anb文件
C#生成anb文件
107 0
C#生成anb文件
小技巧——对比两个文件是否相同
小技巧——对比两个文件是否相同
82 0
|
Windows Linux
八、文件的处理
f = open ('‪H:\\呵呵.txt',encoding='utf-8',mode='r') s = f.read print(s) f.close f:变量,f_obj,file,f_handler,...文件句柄。
1069 0