linux 文件 tell,Linux环境编程之文件目录

文件IO是主要是对一个文件的操作的基本函数,这篇主要描述文件系统的其他特征和文件的性质。

1.stat函数

stat函数返回一个与此命名文件有关的信息结构, fstat函数获得已在描述符 filedes上打开的文件的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。

#include 

#include 

intstat(constchar*pathname,structstat *buf) ;

intfstat(intfiledes,structstat *buf) ;

intlstat(constchar*pathname,structstat *buf) ;

#include

#include

int stat(const char *pathname, struct stat *buf) ;

int fstat(int filedes,struct stat *buf) ;

int lstat(const char *pathname, struct stat *buf) ;

三个函数的返回:若成功则为0,若出错则为-1

stat的数据结构如下所示

structstat {

mode_t st_mode;/* 文件类型和权限 */

ino_t st_ino;/* inode 号*/

nlink_t st_nlink;/* 链接数目 */

uid_t st_uid;/* 文件所有者的uid*/

gid_t st_gid;/* 文件所有者的组id */

off_t st_size;/* 文件大小 */

time_tst_atime;/* 最后访问时间 */

time_tst_mtime;/* 最后修改时间 */

time_tst_ctime;/* 最后文件状态修改时间*/

blksize_t st_blksize;/* 最佳读写I/O块大小 */

blkcnt_t st_blocks;/* 一共有多少个块*/

};

struct stat {

mode_t st_mode; /* 文件类型和权限 */

ino_t st_ino; /* inode 号*/

nlink_t st_nlink; /* 链接数目 */

uid_t st_uid; /* 文件所有者的uid*/

gid_t st_gid; /* 文件所有者的组id */

off_t st_size; /* 文件大小 */

time_t st_atime; /* 最后访问时间 */

time_t st_mtime; /* 最后修改时间 */

time_t st_ctime; /* 最后文件状态修改时间*/

blksize_t st_blksize; /* 最佳读写I/O块大小 */

blkcnt_t st_blocks; /* 一共有多少个块*/

};

2.access函数

access按实际用户ID和实际组ID进行文件权限的测试。

#include 

intaccess(constchar*pathname,intmode) ;

#include

int access(const char *pathname, int mode) ;

返回:若成功则为0,若出错则为-1

参数解析:

mode值可以是以下几种:

R_OK   测试读许可权

W_OK  测试写许可权

X_OK   测试执行许可权

F_OK    测试文件是否存在

3.truncate函数

truncate 把某个文件截短到一定长度,跟open中的O_TRUNC类似。

inttruncate(constchar*pathname, off_t length);

intftruncate(intfiledes, off_t length);

int truncate(const char *pathname, off_t length);

int ftruncate(int filedes, off_t length);

成功返回0 ,失败返回-1

4.与链接有关的函数

link函数用来创建一个硬链接文件

int link(const char *existingpath, const char   *newpath);

symlink用来创建一个软链接文件

int symlink(const char *actualpath, const char   *sympath);

unlink删除软链接时只删除链接文件本身,被连接的文件不受影响。删除硬链接时,如果inode引用数为0,则删除文件。如果有进程还在使用该文件,则可以继续使用。

int unlink(const char *pathname);

readlink只读取软连接本身

ssize_t readlink(const char* restrict pathname,   char *restrict buf, size_t bufsize);

5.utime函数

utime读取文件的最后访问时间和修改实际那

#include 

intutime(constchar*pathname,conststructutimbuf *times);

structutimbuf {

time_tactime;/* 最后访问时间 */

time_tmodtime;/* 最后修改时间 */

}

#include

int utime(const char *pathname, const struct utimbuf *times);

struct utimbuf {

time_t actime; /* 最后访问时间 */

time_t modtime; /* 最后修改时间 */

}

Inode属性的修改时间有内核来完成,应用程序没有权限去设置。

6.目录操作函数

opendir用来打开一个目录 的内容,并返回目录指证。

#include

DIR *opendir(const char *pathname); 成功返回DIR指针,失败返回NULL

readdir 以一定次序读取目录内容,

struct dirent *readdir(DIR *dp);

struct dirent {

ino_t d_ino; /* i-node 号 */

char  d_name[NAME_MAX + 1]; /*文件或目录名*/

}

rewinddir 重置目录的输入流,重置后从第一个目录项开始读。

void rewinddir(DIR *dp);

telldir 返回目录流的当前位置

long telldir(DIR *dp);

seekdir  设置目录流位置。

void seekdir(DIR *dp, long loc);

loc有telldir返回

closedir  关闭目录流

int closedir(DIR *dp);

下面的例子是递归遍历一个目录,打印出这个目录下所有的文件和目录,并且需要打印出这些文件的类型(比如 链接文件,普通文件等)。

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

voiditerate_dir(char*dir );

intmain(intargc,char**argv)

{

if( argc != 2 ){

printf("usage: command dir/n");

return-1;

}

iterate_dir( argv[1] );

return0;

}

voiditerate_dir (char* dir )

{

DIR *cur_dir;

structdirent *dir_ent;

structstat    dir_stat;

charpath[PATH_MAX];

cur_dir = opendir(dir);

if( NULL == cur_dir ){

printf("open dir faild %s/n", strerror(errno));

return;

}

while( NULL != ( dir_ent=readdir(cur_dir))){

if( 0==strcmp(dir_ent->d_name,".") || 0==strcmp( dir_ent->d_name,"..")){

continue;

}

sprintf( path,"%s%s",dir,dir_ent->d_name);

if( -1 == lstat(path, &dir_stat) ){

printf("lstat error %s /n",strerror(errno));

return;

}

if( S_ISDIR( dir_stat.st_mode) ){

strcat(path,"/");

printf("path: %s/n",path);

iterate_dir(path);

continue;

}

printf("file: %s /n", path);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值