Summary Day33

1. 文件管理

1,1 fcntl  函数

(1)F_SETLKW

功能与F_SETLK类似,所不同的是加不上锁并不是返回失败而是等待,直到可以加上该锁为止

(2)F_GETLK

表示试图将第三个参数描述的锁加到第一的参数指定的文件上如果能加上锁但不会去加,而是

将该锁的类型改为F_UNLCK;如果不能加上锁,则将文件中已经存在的锁信息通过第三个参数带出来,

并且将给文件进行加锁的进程号设置到第三个参数的l_pid中


1.2 stat/fstat函数

#include <sys/types.h>

#include <sys/stat.h>

#include <unisth.h>

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

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

int lstat (const char *path, stuct stat *buf);

第一个参数:文件的路劲/文件的描述符

第二个参数:结构体指针,结构体变量的地址

struct stat {

...

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

off_t st_size;//文件的大小

time_t  st_mtime;//文件的最后一次修改时间

...

};

函数的功能:主要用于获取文件的详细信息


类型查找:gcc/cc  -E xxx.c -o xxx.i ,在xxx.i中查找

mode_t undigned int

off_t long int

time_t long int


扩展:

#include <time.h>

char *ctime(const time_t *timep);

=>主要用于将整数类型的时间转换为字符串类型的时间

struct tm *location(const time_t *timpe);

=>主要用于将整数类型转换成结构体类型

struct tm {

int tm_sec; //秒数

int tm_min;//分钟

int tm_hour;//小时

Int tm_mady;//日

int tm_mon;//月+1

int tm_year;//年+1900

int tm_wday;//星期+1

int tm_yday;//年中的天数

int tm_isdst;//夏令时

};


1.3 access函数

#include <unistd.h>

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

第一个参数:文件的路劲是否存在;

第二个参数:操作模式

函数的功能:

主要用于判断文件是否存在以及是否拥有指定的权限


1.4 chmod和truncate

#include <sys/stat.h>

int chmod(const char*path, mode_t mode);

int fchmode(int fd, mode_t mode);

第一个参数:文件的路劲、文件描述符

第二个参数:文件新权限如:0644

函数的功能:主要用于修改文件的权限


#include <unistd.h>

#include <sys/types.h>

int truccate(const char *path, off_t  length);

int ftrucate(int fd, off_t length);

第一个参数:文件的路劲、文件的描述符

第二个参数:文件的最新大小

函数的 功能:主要用于将指定文件大小截取指定大小

1.6 mmap/munmap函数


2. 目录管理

2.1 opendir()函数.

#Include <sys/type.h>

#Include <dirent.h>

DIR *opendir(const char *name);

DIR *fdopendir(int fd);

函数的功能:

主要用于打开参数指定的目录,成功返回指针,失败返回NULL

2.2 readdir函数

#include <dirent.h>

struct sirent *readdir(DIR *dirp);

函数功能:主要用于读取参数指定的目录中内容,成功返回结构体指针,失败返回NULL

2.3 closedir 函数

#include <sys/types.h>

#Include <dirent.h>

int closedir (DIR *dirp)

函数的功能:主要用于关闭参数指定目录

2.4 getcwd函数

#Include <unistd.h>

char *getcwd(char *buf, size_t size);

函数功能:

主要用于获取当前工作目录的绝对路径存放到buf中,buf的大小是size个字节

成功返回buf,失败返回NULL

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值