判断文件是否为空 C++
#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++的更多相关文章
- Linux判断文件是否为空,不为空则打印该文件的大小
Linux判断文件是否为空,不为空则打印该文件的大小,使用到的命令是-s + filename -s filename 如果文件大小大于0,则返回true. 例如: 查看当前目录 # ls -l to ...
- shell判断文件是否为空
[[ `cat a.log |wc -l` -eq 0 ]] && echo "file is empty"
- Python 判断文件是否存在的三种方法
通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. 这里将介绍三种判断文件或文件夹是否存在的方法,分别使用os模块.Try ...
- java中文件是否为空
在File类中并没有提供判断文件是否为空的方法,但可以借助length()方法的返回值进行判断.如果文件不存在或文件为空时,length()方法返回0. File file = new File(&q ...
- linux下使用c判断文件夹是否为空的小程序
自己写了一个 判断文件夹是否为空的小代码 //文件夹操作相关的函数的帮助$: man 3 readdir #include <stdio.h> #include <sys/types ...
- python os 命令,及判断文件夹是否存在
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- 解决POI读取Excel如何判断行是不是为空
在作Excel表导入数据库的时候要统计成功导入了多少条,失败了多少条. 问题一:Excel表里有225行,只有3行是有数据的,但是我在读Excel表的时候它连没有数据的行也读进来了. 问题二:如果你是 ...
- C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)
protected void Button1_Click(object sender, EventArgs e) { if (Directory.Exists(Server.MapPath(" ...
- Linux 获取文件时间信息 判断文件是否存在
获取文件时间戳 (1)查看全部信息: stat e4.txt 范例: [root@localhost ~]# stat e4.txt File: “e4.txt” Size: 0 Blocks: ...
随机推荐
- [Windows Server 2012] Filezilla安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...
- 【译】x86程序员手册12-4.2系统指令
4.2 Systems Instructions 系统指令 Systems instructions deal with such functions as: 系统指令具有以下功能: Verifica ...
- (转) Hibernate注解开发
http://blog.csdn.net/yerenyuan_pku/article/details/70162268 Hibernate注解开发 在Hibernate中我们一般都会使用注解,这样可以 ...
- XML在线转化为JSON
http://www.utilities-online.info/xmltojson/
- 测试edit中数据是否合法
void XyModal::OnEnKillfocusEdit1() { // TODO: 在此添加控件通知处理程序代码 CString cText; GetDlgItemText(IDC_EDIT1 ...
- 写一个 sum方法,在使用下面任一语法调用时,都可以正常工作
console.log(sum(2,3)); // Outputs 5 console.log(sum(2)(3)); // Outputs 5 (至少)有两种方法可以做到: 方法1: functio ...
- 16.2 【C# 5】调用者信息特性
16.2.1 基本行为 .NET 4.5引入了三个新特性(attribute),即 CallerFilePathAttribute . CallerLineNumber- Attribute 和 Ca ...
- c/c++排坑(3) -- c/c++中的switch语句
switch语句的简单介绍 一个 switch 语句允许测试一个变量等于多个值时的情况.每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查. switch(expres ...
- 面向对象:__getitem__、__setitem__、__delitem__
item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...
- Mybatis操作Mysql批量更新的一个坑-&allowMultiQueries=true允许批量更新
前言 利用Mybatis批量更新或者批量插入,实际上即使Mybatis完美支持你的sql,你也得看看你操作的数据库是否完全支持,而同事,最近就遇到这样的一个坑! 问题 先带大家来 ...