linux 文件和目录操作的相关函数

struct stat
{
mode_t    st_mode;    文件类型,文件权限
ino_t     st_ino;        i节点号
dev_t    st_dev;       
dev_t    st_rdev;    设备文件序号
nlink_t    st_nlink;    链接
uid_t    st_uid;
gid_t     st_gid;        用户ID
off_t    st_size;    文件大小,此字段只对普通文件、目录文件和符号连接有意义。
time_t    st_atime;    最后存取时间
time_t    st_mtime;    文件内容的最后修改时间
time_t    st_ctime;    文件状态的最后修改时间
long    st_blksize;   
long     st_blocks;
};


1,stat函数取得文件信息。
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
int fstat (int fd,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);


lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息

2,access函数判断文件权限
#include<unistd.h>
int access (const char *name, int mode) ;

返回:若成功则为 0,若出错则为- 1
access函数的mode常数,取自 <unistd.h>
mode                 说   明
R_OK                  测试读许可权
W_OK                 测试写许可权
X_OK                测试执行许可权
F_OK                测试文件是否存在

3,umask函数设置文件创建屏蔽字
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t task) ;

返回:以前的文件方式创建屏蔽字

4,chmod函数用于修改文件的权限
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);

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


5,chown函数可用于更改文件的用户 ID和组ID。
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);

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

6,在文件末尾处截短文件可以调用函数 truncate和ftruncate。将一个文件的长度截短为 0是一个特例,用O_TRUNC标志可以做到这一点。
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t
length) ;                                       
int ftruncate(int filedes, off_t length) ;

两个函数返回;若成功则为 0,若出错则为- 1


7,创建一个向现存文件连接的方法是使用link函数,想当于硬连接 ln。只有超级用户进程可以创建指向一个目录的新连接。其理由是这样做可能在文件系统中形成循环,大多数处理文件系统的公用程序都不能处理这种情况
#include <unistd.h>
int link(const char*oldpath, const char *newpath) ;

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

为了删除一个现存的目录项,可以调用 unlink函数。
#include <unistd.h>
int unlink(const char *pathname) ;

返回:若成功则为 0,若出错则为-1。此函数删除目录项,并将由 pathname所引用的文件的连接计数减 1。

硬连接的一些限制: ( a )硬连接通常要求连接和文件位于同一文件系统中, ( b )只有超级用户才能创建到目录的硬连接。

symlink函数创建一个符号连接。相当于软连接,ln -s
#include <unistd.h>
int symlink(const char *oldpath, const char *sympath) ;

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

因为open函数跟随符号连接,所以需要有一种方法打开该连接本身,并读该连接中的名字。
readlink函数提供了这种功能。
#include <unistd.h>
int readlink(const char *pathname, char *buf, int bufsize) ;

返回:若成功则为读的字节数,若出错则为- 1
此函数组合了open, read和close的所有操作。

8,用mkdir函数创建目录,用 rmdir函数删除目录。
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode) ;

返回:若成功则为 0,若出错则为- 1
#include <unistd.h>
int rmdir(const char *pathname) ;

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

9,remove函数解除对一个文件或目录的连接。对于文件, remove的功能与unlink相同。对于目录, remove的功能与rmdir相同。
#include <stdio.h>
int remove(const char *pathname) ;

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

文件或目录用rename函数更名
#include <stdio.h>
int rename(const char *oldname, const char *newwname) ;

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


10,一个文件的存取和修改时间可以用 utime函数更改
#include <sys/types.h>
#include <utime.h>
int utime (const char *name, const struct utimebuf *t);

返回:若成功则为 0,若出错则为- 1
如果times是一个空指针,则存取时间和修改时间两者都设置为当前时间;
如果times是非空指针,则存取时间和修改时间被设置为 times所指向的结构中的值。此时,进程的有效用户ID必须等于该文件的所有者 ID,或者进程必须是一个超级用户进程。对文件只具有写许可权是不够的

此函数所使用的结构是:
struct utimbuf {
time_t actime;                /*access time*/
time_t modtime;               /*modification time*/
}

11,对文件目录的操作函数,opendir readdir rewinddir

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname) ;

返回:若成功则为指针,若出错则为 NULL

struct dirent *readdir(DIR *dr);
返回:若成功则为指针,若在目录尾或出错则为 NULL

void rewinddir(DIR *dr);
重置读取目录的位置为开头

i nt close(DIR *dr); 返回:若成功则为 0,若出错则为- 1

定义在头文件<dirent.h>中的dirent结构与实现有关。 此结构至少包含下列两个成员:
struct dirent {
    ino_t d_ino;
    char d_name[NAME_MAX+1];
}

12,chdir,改变当前目录
#include<unistd.h>
int chdir(const char *pathname);
int pchdir(int fd);


getcwd,得到当前目录的完整路径.
#include<unistd.h>
char *getcwd(char *buf, size_t size);

若失败返回NULL, buf为存储路径的字符数组,size为长度

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值