#include <sys/stat.h>

int stat(const char *restrict pathname, struct stat *restrict buf);

struct stat {

  mode_t st_mode;     /*file type & mode(permissions)*/

  ino_t st_ino;         /*i-node number(serial number)*/

  dev_t st_dev;          /*device number(file system)*/

  dev_t st_rdev;        /*device number for special files*/

  nlink_t st_nlink;    /*number of links*/

  uid_t st_uid;          /*user ID of owner*/

  gid_t st_gid;       /*group id of owner*/

  off_t st_size;         /*size in bytes, for regular files*/

  struct timespec st_atime;  /*time of last access*/

  struct timespec st_mtime; /*time of last modification*/

  struct timespec st_ctime;  /*time of last file status chage*/

  blksize_t st_blksize;         /*best I/O block size*/

  blkcnt_t st_blocks;     /*number of disk blocks allocated*/

};

一旦给出pathname,stat函数将返回与此命名文件有关的信息结构,根据大小信息判断文件是否为空。

源代码如下:

#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;

  if(size == 0)
    return true;
  else
    return false;
}

int main() {
  std::string file_path="/root/vm.data";

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

  return 0;
}

判断文件是否为空 C++的更多相关文章

  1. Linux判断文件是否为空,不为空则打印该文件的大小

    Linux判断文件是否为空,不为空则打印该文件的大小,使用到的命令是-s + filename -s filename 如果文件大小大于0,则返回true. 例如: 查看当前目录 # ls -l to ...

  2. shell判断文件是否为空

    [[ `cat a.log |wc -l` -eq 0 ]] && echo "file is empty"

  3. Python 判断文件是否存在的三种方法

    通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. 这里将介绍三种判断文件或文件夹是否存在的方法,分别使用os模块.Try ...

  4. java中文件是否为空

    在File类中并没有提供判断文件是否为空的方法,但可以借助length()方法的返回值进行判断.如果文件不存在或文件为空时,length()方法返回0. File file = new File(&q ...

  5. linux下使用c判断文件夹是否为空的小程序

    自己写了一个 判断文件夹是否为空的小代码 //文件夹操作相关的函数的帮助$: man 3 readdir #include <stdio.h> #include <sys/types ...

  6. python os 命令,及判断文件夹是否存在

    使用前 import os导入模块   os模块: os.sep     可以取代操作系统特定的路径分割符 os.linesep  字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...

  7. 解决POI读取Excel如何判断行是不是为空

    在作Excel表导入数据库的时候要统计成功导入了多少条,失败了多少条. 问题一:Excel表里有225行,只有3行是有数据的,但是我在读Excel表的时候它连没有数据的行也读进来了. 问题二:如果你是 ...

  8. C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)

    protected void Button1_Click(object sender, EventArgs e) { if (Directory.Exists(Server.MapPath(" ...

  9. Linux 获取文件时间信息 判断文件是否存在

    获取文件时间戳   (1)查看全部信息: stat e4.txt 范例: [root@localhost ~]# stat e4.txt File: “e4.txt” Size: 0 Blocks: ...

随机推荐

  1. html5——3D案例(立方体)

    立方体:父盒子规定了3d呈现属性,立方体做旋转运动 移动顺序:1.每个盒子都先移动100px,然后再做相应的旋转  2.只有这样立方体的几何中心点与父盒子的几何中心点是一样的 <!DOCTYPE ...

  2. mysql常用命令介绍

    mysql适用于在Internet上存取数据,支持多种平台 1.主键:唯一标识表中每行的这个列,没有主键更新或删除表中的特定行很困难. 2.连接mysql可以用Navicat 要读取数据库中的内容先要 ...

  3. DateTimePicker 控件置空

    dtOrderDateFrom.Format = DateTimePickerFormat.Custom; dtOrderDateFrom.CustomFormat = " "; ...

  4. TP调用JS

    echo "<script>alert('删除成功');window.location.href='?c=Banner&a=index' </script>& ...

  5. nginx+tomcat实现负载均衡集群

    一台tomcat站点由于可能出现单点故障及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境下 所以需要一套可靠的解决方案来完善web站点架构 而Nginx只能访问静态页面,如果需要动态需要 ...

  6. 考试T1总结(又CE?!)

    考试T1CE... 最近不适合考试 T1 扶苏是个喜欢一边听古风歌一边写数学题的人,所以这道题其实是五三原题.歌曲中的主人公看着墙边的海棠花,想起当年他其实和自己沿着墙边种了一排海棠,但是如今都已枯萎 ...

  7. DOM节点的获取

      document.getElementById();//id名,在实际开发中较少使用,选择器中多用class  id一般只用在顶级层存在 不能太过依赖id document.getElements ...

  8. Django - 一对多数据示例

    1.增加Host -id 可以在模版中增加代码: 备注: 1.counter (从1开始) 2.counter0(从0开始) 3.revcounter(倒序) 4.revcounter0(倒序从0开始 ...

  9. SGU100

    Read integers A and B from input file and write their sum in output file. Input Input file contains ...

  10. linux中文man手册安装

    1.下载源码 源码网址 https://src.fedoraproject.org/repo/pkgs/man-pages-zh-CN/ 下载源码 wget https://src.fedorapro ...