Bootstrap

C++ Linux 判断文件是否为空

#include <iostream>
#include <sys/stat.h>

bool file_is_empty(std::string &file_path)
{
    struct stat buf{};

    stat(file_path.c_str(), &buf);

    size_t size = buf.st_size;

    return size == 0;
}

int main()
{
    std::string file_path = "/home/dennis/IsEmpty.txt";

    if (file_is_empty(file_path))
        std::cout << file_path << " is empty\n";
    else
        std::cout << file_path << " is not empty\n";

    return 0;
}
;