#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. JS——选择水果

    注意点: 1.select标签size属性显示选项数组,multiple属性可以多选 2.原select节点下的子节点在移动到其他selec标签下的时候,其原来的select标签下子节点长度在发生变化 ...

  2. 正则表达式提取String子串

    最近遇到了一个字符串处理的功能,忽然发现了正则表达式的强大,深深的被她的迷人魅力所吸引,以前只是听说,今天亲眼所见,亲身经历,真的彻底折服. 言归正传:java中String类里面封装了很多字符串处理 ...

  3. Redis系列(一)StackExchange.Redis的使用

    Redis系列(一)StackExchange.Redis的使用 一.DLL安装 用NuGet搜索StackExchange.Redis,然后下载就可以. ConnectionMultiplexer对 ...

  4. PHP 之微信JSSDK类封装

    <?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...

  5. Linux 之CentOS7使用firewalld打开关闭防火墙与端口

    一.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  6. UpdateData

    UpdateData 类似于C语言的scanf printf函数 管理控件与关联变量之间的数据更新. updatedata(true)把界面输入的数值更新到关联变量中,updatedata(false ...

  7. .net core 使用 textSharp生成pdf

    引入Nuget包 using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.IO; namespace ...

  8. H5及微信中唤起app的解决方案

    今天我们就来说说这个callapp-lib 我的接到的需求大概是这样的 如果检测到不是在app里面用webview打开的页面就会显示上面的立即打开按钮, 点击的话会判断是否在微信中, 如果在微信中打开 ...

  9. 原生Ajax的使用——含开放API接口

    看了两天关于Ajax的使用,总感觉云里雾里的. 故在此总结梳理一下,如果疏漏错误还请纠正支出. Ajax能够在向服务器请求额外的数据时,不必重新加载/卸载整个页面,实现一小块区域性的刷新,也是常说的异 ...

  10. 使用IDM下载软件下载百度云网盘里的资源,以Chrome浏览器为例

    1.下载安装最新版的Chrome浏览器,我是在腾讯软件下载中心下载的,使用普通下载,下载地址:https://dl.softmgr.qq.com/original/Browser/72.0.3626. ...