您的位置:首页
获得文件属性的函数调用实例
2017-04-25 19:55
295 查看
获取文件属性信息,在终端下能够直接利用ls- l命令,在编程时,用到下面三个函数:
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int stat(const char *file_name, struct stat *buf);
函数说明: 通过文件名称filename获取文件信息,并保存在buf所指的结构体stat中
返回值: 运行成功则返回0,失败返回-1,错误代码存于errno
错误代码:
ENOENT 參数file_name指定的文件不存在
ENOTDIR 路径中的文件夹存在但却非真正的文件夹
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 參数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 參数file_name的路径名称太长
int fstat(int filedes, struct stat *buf);
int lstat(const char *file_name, struct stat *buf);
struct stat {
dev_t st_dev; //文件的设备编号
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目。刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次訪问时间
time_t st_mtime; //最后一次改动时间
time_t st_ctime; //最后一次改变时间(指属性)
};
#include <sys/stat.h>
#include <unistd.h>
mian()
{
struct stat buf;
stat (“/etc/passwd”,&buf);
printf(“/etc/passwd file size = %d /n”,buf.st_size);
}
编程实例1:
编程实例2:
补充:
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int stat(const char *file_name, struct stat *buf);
函数说明: 通过文件名称filename获取文件信息,并保存在buf所指的结构体stat中
返回值: 运行成功则返回0,失败返回-1,错误代码存于errno
错误代码:
ENOENT 參数file_name指定的文件不存在
ENOTDIR 路径中的文件夹存在但却非真正的文件夹
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT 參数buf为无效指针,指向无法存在的内存空间
EACCESS 存取文件时被拒绝
ENOMEM 核心内存不足
ENAMETOOLONG 參数file_name的路径名称太长
int fstat(int filedes, struct stat *buf);
int lstat(const char *file_name, struct stat *buf);
struct stat {
dev_t st_dev; //文件的设备编号
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目。刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次訪问时间
time_t st_mtime; //最后一次改动时间
time_t st_ctime; //最后一次改变时间(指属性)
};
#include <sys/stat.h>
#include <unistd.h>
mian()
{
struct stat buf;
stat (“/etc/passwd”,&buf);
printf(“/etc/passwd file size = %d /n”,buf.st_size);
}
编程实例1:
#include <sys/stat.h> #include <unistd.h> mian() { struct stat buf; stat (“/etc/passwd”,&buf); printf(“/etc/passwd file size = %d /n”,buf.st_size); }
编程实例2:
#include<stdio.h> #include<unistd.h> #include<time.h> #include<sys/types.h> #include<sys/stat.h> int main(int argc,char *argv[]) { struct stat buf; if(argc!=2) printf("input the parameter error!\n"); if(stat(argv[1],&buf)==-1) printf("get the stat is faild!\n"); printf("st_dev:%d\n",buf.st_dev); printf("st_ino:%d\n",buf.st_ino); printf("st_mode:%d\n",buf.st_mode); printf("st_nlink:%d\n",buf.st_nlink); printf("st_uid:%d\n",buf.st_uid); printf("st_gid:%d\n",buf.st_gid); printf("st_rdev:%d\n",buf.st_rdev); printf("st_size:%d\n",buf.st_size); printf("st_blksize:%d\n",buf.st_blksize); printf("st_atime:%s\n",ctime(&buf.st_atime)); printf("st_mtime:%s\n",ctime(&buf.st_mtime)); printf("st_ctime:%s\n",ctime(&buf.st_ctime)); return 0; }
补充:
unsigned long get_file_size(const char *path) { unsigned long filesize = -1; FILE *fp; fp = fopen(path, "r"); if(fp == NULL) return filesize; fseek(fp, 0L, SEEK_END); filesize = ftell(fp); fclose(fp); return filesize; }
相关文章推荐
- 获得文件属性的函数调用实例
- VC调用matlab中定义的.m文件中的函数的实例
- python基础-面向对像、类名\实例调用、__init__函数、类名\实例增删改查属性
- C# 获得当前文件夹内所有文件的名称,大小,类型,属性,日期的函数
- VC调用matlab中定义的.m文件中的函数的实例
- Linux获取文件属性stat()、fstat()、lstat()函数实例学习
- stat获得文件属性的函数
- VC调用matlab中定义的.m文件中的函数的实例
- VC调用matlab中定义的.m文件中的函数的实例
- VC调用matlab中定义的.m文件中的函数的实例
- [Linux文件属性]使用stat函数获得指定文件的属性
- VC调用matlab中定义的.m文件中的函数的实例
- Inside VCL:接口指针调用函数的时候,如何获得对象指针以完成函数调用?
- [Delphi] 计算目录大小的函数,获得目录文件列表,计算文件的大小
- 记录文件实例与调用
- 类中的虚函数,通过指针获得vtable内容,然后通过函数指针指向虚函数并调用
- 客户端获得上传文件大小的简单实例
- 调用windows文件属性对话框
- 目录,文件名,后缀名,文件大小,文件属性函数
- C# 调用声音文件,包括API函数和调用VisualBasic(vs2003)函数