#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. html——导航demo

    通过行内块.伪类对导航栏进行设置 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  2. 控制台——EventLog实现事件日志操作

    我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...

  3. javascript常用功能函数

    特殊字符转义:将<, >, &, “进行转义 function escape(str){ return str.replace(/[<>"&]/g,f ...

  4. Centos7搭建nginx并提供外网访问

    搭建nginx之后,80端口,其他机器无法访问 查询端口是否开启 firewall-cmd --query-port=80/tcp 永久开放80端口 firewall-cmd --permanent ...

  5. cannot load oci dll,193-navicate连接oracle的解决方法

    navicat连接远程数据库时 是因为instantclient是64位的,navicat是32位的,两者要一致 因为自己的navicat premium是破解版的,所以就另找了instantclie ...

  6. 8 switch case

    当一个case成立,从这个case向后穿透所有case,即使后面的case条件不成立 包括default,直到程序结束或者遇到break程序才结束. 1.case是常量,且不能重复 2.表达式可以是b ...

  7. Web 常用

    System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();

  8. js-2018-11-01 关于break和continue语句

    1.label语句 语法:label: statement 加标签语句一般都要与for语句等循环语句配合使用. 2.break语句 立即退出循环,强制执行循环后面的语句. 3.continue语句 立 ...

  9. (C/C++学习)7.数组及其访问方式

    说明:数组的数据类型是一种构造类型,而存储数组的内存是一段连续的存储区域.数组的数据类型决定了连续内存的访问方式,它包括数组的三要素:起始地址.步长以及元素个数. 一.一维数组 1.形式:type 数 ...

  10. Git:分支的创建、合并、管理和删除

    了解分支 如果想实现多人协作.划出Bug区.Feature区等功能,就需要分支功能.(确实很强大的地方) 每次commit时,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条 ...