通过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 获取 文件的大小的更多相关文章

  1. Linux C++获取文件夹大小

    项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...

  2. Linux查看一个文件夹大小

    1.Linux查看一个文件夹大小: du -sh /home/yangkun [yangkun@sg1 bin]$ du -sh /home/yangkun/ 164M /home/yangkun/ ...

  3. Delphi获取文件的大小(实际&物理)

    源:获取文件的大小(实际&物理) class function TDuoFile.GetFileSize(const AFile: TFileName): Int64; var sr:TSea ...

  4. linux下C获取文件的大小

    获取文件大小这里有两种方法: 方法一. 范例: unsigned long get_file_size(const char *path) { unsigned long filesize = -1; ...

  5. window/linux下获取文件MD5

    MD5消息摘要算法(英语: MD5 Message-Digest Algorithm), 主要用于确保信息传输过程的一致性校验.   首先介绍两个工具: window: WinMD5Free Linu ...

  6. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  7. seek和tell的用法--获取文件内容大小(字节)

    /*获取文件中存取的数据内容的大小(字节数) ellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get ...

  8. Linux 查看各文件夹大小命令du -h --max-depth=1

    du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>] [--max-de ...

  9. linux 下获取文件最后几行

    在Linux下,获取文件倒数几行的命令是: tail -n 10 your_filename     #获取倒数10行

随机推荐

  1. NGUI和UGUI动画不能设置alpha值的问题

    动画播放alpha参数改变但无实际画面效果,原因是要挂一个脚本,设置实时更新数据. NGUI void Update() { widget.SetDirty(); } UGUI void Update ...

  2. Redis 学习笔记四 Mysql 与Redis的同步实践

    一.测试环境在Ubuntu kylin 14.04 64bit 已经安装Mysql.Redis.php.lib_mysqludf_json.so.Gearman. 点击这里查看测试数据库及表参考 本文 ...

  3. What is purpose of @ConditionalOnProperty annotation?

    http://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation **** ...

  4. 区别:Use MFC In A Shared DLL 和 Use MFC In A Static Library

    摘自:Programming Windows with MFC, 2nd Edition Choosing Use MFC In A Shared DLL minimizes your applica ...

  5. python学习笔记(6)--有道翻译爬虫

    说明: 1. 导入三个模块,urllib.request.urlopen用来打开url链接,urllib.parse的urlencode方法将浏览器network里的data对象转为urlopen的第 ...

  6. [Busybox]Busybox制作文件系统

    问题: 1.目前busybox和bootstrap两种方案制作文件系统,哪种开发周期更短,更加简单? 2.如果需要在文件系统中添加某个package,要怎么做,如vim/udhcpd等? 转自:htt ...

  7. MapReduce的InputFormat过程的学习

    转自:http://blog.csdn.net/androidlushangderen/article/details/41114259 昨天经过几个小时的学习,把MapReduce的第一个阶段的过程 ...

  8. mongodb和redis设计原理简析

    转自:http://blog.csdn.net/yangbutao/article/details/8309539 redis:   1.NIO通信     因都在内存操作,所以逻辑的操作非常快,减少 ...

  9. 安卓解析json

    重点是开启网络权限 难点是调用函数 开启网络权限 </application> <uses-permission android:name="android.permiss ...

  10. 用newInstance与用new是区别的

    用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有两种创建对象方式?这个就要从可伸缩.可扩展,可重用等软件思想上解释了.Java中工厂模式经 ...