视屏网站制作,公司网站可以自己做么,net网站开发net网站开发,视频网站设计论文目录 1. 前言 2. 函数介绍 2.1 访问目录 – opendir 2.2 访问目录 – readdir 2.3 访问目录 – closedir 2.4 修改文件访问权限 – chmod/fchmod 2.5 获取文件属性 – stat/lstat/fstat 2.5.1 文件属性 – struct stat 2.6 文件类型 – st_mode 3. 代码练习 3.1 要求 3.2 代… 目录 1. 前言 2. 函数介绍 2.1 访问目录 – opendir 2.2 访问目录 – readdir 2.3 访问目录 – closedir 2.4 修改文件访问权限 – chmod/fchmod 2.5 获取文件属性 – stat/lstat/fstat 2.5.1 文件属性 – struct stat 2.6 文件类型 – st_mode 3. 代码练习 3.1 要求 3.2 代码实现 3.2.1 依赖库文件 3.2.2 声明子函数 3.2.3 实现子函数 3.2.4 main函数实现 5. 总结 1. 前言
本文通过介绍文件目录以及文件属性相关函数进行目录操作和文件属性获取。
2. 函数介绍
2.1 访问目录 – opendir
opendir函数用来打开一个目录文件: #include dirent.hDIR *opendir(const char *name);DIR *fdopendir(int fd); 使用文件描述符要配合open函数使用
DIR是用来描述一个打开的目录文件的结构体类型成功时返回目录流指针出错时返回NULL
2.2 访问目录 – readdir
readdir函数用来读取目录流中的内容: #include dirent.hstruct dirent *readdir(DIR *dirp);
struct dirent是用来描述目录流中一个目录项的结构体类型包含成员char d_name[256] 参考帮助文档成功时返回目录流dirp中下一个目录项出错或到末尾时时返回NULL
2.3 访问目录 – closedir
closedir函数用来关闭一个目录文件: #include dirent.hint closedir(DIR *dirp); 成功时返回0出错时返回EOF
2.4 修改文件访问权限 – chmod/fchmod
chmod/fchmod函数用来修改文件的访问权限: #include sys/stat.hint chmod(const char *path, mode_t mode);int fchmod(int fd, mode_t mode);
成功时返回0出错时返回EOFroot和文件所有者能修改文件的访问权限示例 chmod(“test.txt”, 0666);
2.5 获取文件属性 – stat/lstat/fstat
stat/lstat/fstat函数用来获取文件属性: #include sys/stat.hint stat(const char *path, struct stat *buf);int lstat(const char *path, struct stat *buf);int fstat(int fd, struct stat *buf);
成功时返回0出错时返回EOF如果path是符号链接stat获取的是目标文件的属性而lstat获取的是链接文件的属性
2.5.1 文件属性 – struct stat
struct stat是存放文件属性的结构体类型:
struct stat {dev_t st_dev; //文件的设备编号ino_t st_ino; //节点mode_t st_mode; //文件的类型和存取的权限nlink_t st_nlink; //连到该文件的硬连接数目刚建立的文件值为1uid_t st_uid; //用户IDgid_t st_gid; //组IDdev_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; //最后一次改变时间(指属性)
};2.6 文件类型 – st_mode
通过系统提供的宏来判断文件类型: S_ISREG(st_mode) 是否常规文件S_ISDIR(st_mode)是否目录S_ISCHR(st_mode)是否字符设备S_ISBLK(st_mode)是否块设备S_ISFIFO(st_mode)是否FIFO文件S_ISLNK(st_mode)是否链接文件S_ISSOCK(st_mode)是否SOCKET文件
通过系统提供的宏来获取文件访问权限:
S_IRUSR所有者有读权限S_IWUSR所有者拥有写权限S_IXUSR所有者拥有执行权限S_IRGRP群组拥有读权限S_IWGRP群组拥有写权限S_IXGRP 群组拥有执行权限S_IROTH其他用户拥有读权限S_IWOTH其他用户拥有写权限S_IXOTH其他用户拥有执行权限
3. 代码练习
3.1 要求
遍历一个文件夹下所有文件并打印文件大小和日期
3.2 代码实现
3.2.1 依赖库文件
#include dirent.h
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include unistd.h
#include time.h
3.2.2 声明子函数
void ReadDir(DIR* dp);
3.2.3 实现子函数
若读到的目录项为.或..则跳过
判定是否为常规文件若是则打印文件大小和日期。
注意这里的stat若打开除程序允许目录的其他文件夹中的文件其中path变量不能只是当前目录下的文件名那样会出现未找到文件或者目录的错误应该是文件的绝对路径。
void ReadDir(DIR* dp)
{struct dirent *dt;int ret;while((dt readdir(dp))!NULL){struct stat buf;if((strcmp(dt-d_name,..)!0)(strcmp(dt-d_name,.)!0)){printf(%s,dt-d_name);}ret stat(dt-d_name,buf);if(ret0){perror(stat);return;}if(S_ISREG(buf.st_mode)){printf( %d,(int)buf.st_size);struct tm* t;t localtime(buf.st_ctime);printf( %d-%d-%d %d:%d\n,t-tm_year1900,t-tm_mon1,t-tm_mday,t-tm_hour,t-tm_min);}else{printf(\n);}}return;
}
3.2.4 main函数实现
int main(int argc,char* argv[])
{DIR* dp;struct dirent *dt;dp opendir(/home/linux/Lv5/Day9/);if(dp NULL){perror(opendir);return 0;}ReadDir(dp);closedir(dp);return 0;
}4. 测试 5. 总结
本文介绍了文件目录以及文件属性相关函数并通过代码练习实现了目录和文件属性获取。 文章转载自: http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.qbfs.cn.gov.cn.qbfs.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.ngkgy.cn.gov.cn.ngkgy.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn