Linux C 获取 文件的大小
通过Linux C库函数来获取文件的大小
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
void main(int argc, char*argv[]){
off_t file_size;
char *buffer;
struct stat stbuf;
int fd;
fd = open(argv[1], O_RDONLY);
if (fd == -1) {
/* Handle error */
}
if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) {
/* Handle error */
}
file_size = stbuf.st_size;
printf("file_size is %d\n", file_size);
}
其中,struct stat的结构如下:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
Linux C 获取 文件的大小的更多相关文章
- Linux C++获取文件夹大小
项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...
- Linux查看一个文件夹大小
1.Linux查看一个文件夹大小: du -sh /home/yangkun [yangkun@sg1 bin]$ du -sh /home/yangkun/ 164M /home/yangkun/ ...
- Delphi获取文件的大小(实际&物理)
源:获取文件的大小(实际&物理) class function TDuoFile.GetFileSize(const AFile: TFileName): Int64; var sr:TSea ...
- linux下C获取文件的大小
获取文件大小这里有两种方法: 方法一. 范例: unsigned long get_file_size(const char *path) { unsigned long filesize = -1; ...
- window/linux下获取文件MD5
MD5消息摘要算法(英语: MD5 Message-Digest Algorithm), 主要用于确保信息传输过程的一致性校验. 首先介绍两个工具: window: WinMD5Free Linu ...
- 【转】linux C++ 获取文件信息 stat函数详解
stat函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h>定义函数: int stat ...
- seek和tell的用法--获取文件内容大小(字节)
/*获取文件中存取的数据内容的大小(字节数) ellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get ...
- Linux 查看各文件夹大小命令du -h --max-depth=1
du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>] [--max-de ...
- linux 下获取文件最后几行
在Linux下,获取文件倒数几行的命令是: tail -n 10 your_filename #获取倒数10行
随机推荐
- SCUT入门-环境搭建
SCUT是一款基于C#且开源的游戏服务端框架,并且有一定的上线项目.最近正在入门中... 1.安装 去官网可以直接下载安装版:http://www.scutgame.com/ 源代码建议OSC Chi ...
- 使用jvisualvm进行远程监控
1. 描述 jvisualvm是JDK自带的监控工具,位于JDK bin目录下面. 默认jvisualvm不能监控远程的JVM进程,需要进行一些配置. 2. 单独在应用上配置JVM启动参数 在Java ...
- sql语句判断是否为数字、字母、中文
1. sql语句判断是否为数字.字母.中文 select ascii(字段) 数字:48-57字母:65-123汉字:123+ 如,要删除某个全为数字的字段 DELETE FROM table WH ...
- 一款基于jQuery和CSS3炫酷3D旋转画廊特效插件
这是一款效果炫酷的jQuery和CSS3 3D旋转画廊特效插件.该3D画廊插件可以通过前后导航按钮来切换图片,效果就像旋转木马一样.它还带有点击放大图片,显示图片标题和用键盘操作等功能. 在线预览 ...
- 应用DataAdapter对象更新数据库中的数据
using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form ...
- 1.thinkphp 权限分析
一.权限分析 用户表 user角色表 role权限表 node think_acces role_id 用户组idnode_id 节点IDlevel 节点等级pid 父级ID THINK_ACCESS ...
- 面向对象设计原则三:里氏替换原则(LSP)
里氏替换原则(LSP)定义:在任何父类出现的地方都可以用它的子类类替换,且不影响功能.解释说明:其实LSP是对开闭原则的一个扩展,在OO思想中,我们知道对象是由一系列的状态和行为组成的,里氏替换原则说 ...
- 使用GitHub建立个人网站
使用GitHub建立个人网站 1 Git简介 2 为什么使用Github Pages 3 创建Github Pages 3.1 安装git工具. 3.2 两种pages模式 3.3 创建步骤 3.4 ...
- Differential Geometry之第七章曲面的若干整体性质
1.曲面的整体描述 2.整体的Gauss-Bonnet公式 2.1.曲面的三角剖分 2.2.Gauss-Bonnet公式 = 2.3.Gauss-Bonnet定理的应用 2.3.1.切向量场的指数定理 ...
- python 人脸识别
"""Performs face alignment and calculates L2 distance between the embeddings of image ...