#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——DOM扩展

    元素获取 1.document.getElementsByClassName ('class') 通过类名获取元素,以类数组形式存在. 2.document.querySelector(‘div’) ...

  2. eclipse中代码整体左右移动的方法

    1.向左:将要移动的代码选中,然后按TAB键2.向右:将要移动的代码选中,然后按shift+tab键 kettas:  2009-8-21

  3. 【Centos7】Tomcat安装及一个服务器配置多个Tomcat

    完成解压 参考 http://www.cnblogs.com/h--d/p/5074800.html https://www.cnblogs.com/tudou-22/p/9330875.html 步 ...

  4. vue04 组件化开发 Vue自动化工具

    5. 组件化开发 5.1 组件[component] 组件(Component)是自定义封装的功能.在前端开发过程中,经常出现多个网页的功能是重复的,而且很多不同的网站之间,也存在同样的功能. 而在网 ...

  5. 使用Oracle函数在创建表的时候自动加入生成的流水号 生成格式是:前缀+年月日+00000

    CREATE OR REPLACE FUNCTION fn_no_make(v_type VARCHAR2, v_number_col VARCHAR2, v_table_name VARCHAR2) ...

  6. [C++] 配平化学方程式算法的封装

    有人已经实现了配平的方法,在此不再重复介绍. https://www.cnblogs.com/Elfish/p/7631603.html 但是,上述的方法所提供的代码还是存在着问题,需要进一步修改. ...

  7. Python2 下Ubuntu linux Mac 安装 PyV8

    在pip install PyV8(注意区分大小写)时,出现了如下报错 command 'x86_64-linux-gnu-gcc' failed with exit status 1 解决方案: 百 ...

  8. 从零开始的 webpack4 + vue2.x

    新建文件夹 webpack-vue 安装依赖 yarn init //初始化package.json yarn add webpack webpack-cli //添加webpack.webpack- ...

  9. LINQ简记(2):重要概念

    为了能让初学者更快速地掌握,在系列文章中,我尽可能地避开理论讲解,一则对于入门者来说,过多的理论叙述反而会降低大家学习编程的兴趣,二则,官方文档的资料很详细,我说了也是废话.因此,我会尽可能地多举些简 ...

  10. 第十四节:pandas之merge()合并

    Pandas提供了一个merge()函数,作为DataFrame对象之间所有标准数据库连接操作的入口pandas.merge(left,right,how='inner',on=None,left_o ...