您的位置:首页 > 运维架构 > Linux

Linux下获取文件大小(程序)

2013-04-23 17:28 537 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/*
Linux终端输入提示符下输入命令:man 2 stat

stat, fstat, lstat - get file status

[b]有点类似于exec函数族一样的,stat函数族。
[/b]
int stat(const char* path, struct stat* buf);
int fstat(int fd, struct stat* buf);
int lstat(const char* path, struct stat* buf);

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
};
*/

int main(int arc, char* const argv[])
{
struct stat fileInfo;
if (stat("main.c", &fileInfo) == 0)
printf("The file size: %d byte!\n", fileInfo.st_size);

return 0;
}


[b]总结:发现这个程序去掉#include <unistd.h>,也可以在Windows下运行。[/b]

[b]仔细一查,Winows也有sys/types.h和sys/stat.h文件,struct stat的定义也差不多。[/b]

[b]额,有点惊喜,Windows也遵循POSIX标准哈![/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: