Linux下目录文件操作

1. 文件操作

- **标准IO和文件IO**:介绍了标准IO和文件IO两种文件操作方式。
- **打开文件**:使用`FILE *fp`和`int fd`来打开文件。
- **读写文件**:使用`fgetc/fputc`、`fgets/fputs`、`fread/fwrite`等函数进行文件的读写操作。
- **关闭文件**:使用`fclose`和`close`函数关闭文件。

文件操作
标准IO文件IO
打开文件FILE* fp

int fd

读写文件

fgetc/fputc

fgets/fputs

fread/fwrite

read/write
关闭文件fcloseclose

2. 目录操作

- **目录文件**:将目录视为特殊类型的文件,通过目录流指针进行操作。
- **打开目录**:使用`opendir`函数打开目录,获取目录流指针。
- **读取目录**:使用`readdir`函数从目录流中读取文件信息。
- **关闭目录**:使用`closedir`函数关闭目录流。

3. 目录操作函数

- **opendir**:打开目录并返回目录流指针。

DIR * opendir(const char *name);  //cd /home/linux/tmp 
功能:
    打开一个目录获得一个目录流指针
参数:
    name:目录名
返回值:
    成功 返回目录流指针
    失败 返回NULL

- **readdir**:从目录流中读取文件信息,返回包含文件信息的结构体指针。

struct dirent *readdir(DIR *dirp);
功能:
    从目录流中读取文件信息并将保存信息的结构体
    地址返回
参数:
    dirp:目录流指针
返回值:
    成功   包含文件信息的结构体指针 
    出错或者读到目录流末尾返回 NULL

On Linux, the dirent structure is defined as follows:

	   struct dirent {
		   ino_t          d_ino;       /* index node number * 索引节点号/
		   off_t          d_off;       /* offset to the next dirent */
		   unsigned short d_reclen;    /* length of this record */
		   unsigned char  d_type;      /* type of file;
                                		   not supported
										  by all file system types */
		   char           d_name[256]; /* filename */
	   };

- **closedir**:关闭目录流。

int closedir(DIR *dirp);
 功能:关闭之前已经打开的目录流对象
 参数:
       opendir的返回结果中目录流对象
 返回值:
        成功  0
         失败   -1;

- **chdir**:改变工作路径。

chdir ("/home/linux"); "../../"
fopen("1.mp4")
int chdir(const char *path);// /home/linux
功能:
    改变当前程序的工作路径
参数:
    path:改变到的路径
返回值:
    成功返回0
    失败返回-1

- **getcwd**:获得当前工作路径。

char *getcwd(char *buf, size_t size);
功能:
    获得当前的工作路径
参数:
    buf:保存工作路径空间的首地址
    size:保存路径空间的长度
返回值:
    成功返回包含路径空间的字符串首地址
    失败返回NULL

- **mkdir**:创建一个目录。

int mkdir(const char *pathname, mode_t mode);//777  666 --x--x--x
功能:
    创建一个目录
    666-
参数:
    pathname:路径
    mode:
        mode & ~umask  0002
        
返回值:
    成功返回0
    失败返回-1

- **rmdir**:删除一个空目录文件。

int rmdir(const char *pathname);
功能:
    删除一个空目录文件
参数:
    pathname:目录文件的名字
返回值:
    成功返回0
    失败返回-1

4. 文件属性

- **文件属性获取**:使用`stat`、`fstat`、`lstat`函数获取文件属性。

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

//软链接 --- lstat  获取软链接这个链接文件本身的属性信息,而不是连接到的目标文件的属性信息。

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

功能:
    获得文件的属性
参数:
    path: 文件的路径
    buf:  属性存放空间的首地址
返回值:
    成功返回0
    失败返回-1

- **文件属性结构体**:`struct stat`包含文件的各种属性,如文件类型、权限、硬链接数、所有者、组、大小、最后修改时间等。

	   All of these system calls return a stat structure, which contains the following fields:

           struct stat {
               dev_t     st_dev;     /* ID of device containing file */ unsigned long int 
               ino_t     st_ino;     /* inode number */(*)   unsigned long int              
               mode_t    st_mode;    /* protection */(* 权限信息) unsigned int 
               nlink_t   st_nlink;   /* number of hard links */(* 硬链接数)unsigned int
               uid_t     st_uid;     /* user ID of owner */ (*UID 用户id)unsigned int
               gid_t     st_gid;     /* group ID of owner */(*group 所在组ID)unsigned int
               dev_t     st_rdev;    /* device ID (if special file) */unsigned long long int
               off_t     st_size;    /* total size, in bytes */(*文件的大小)// long unsigned int
               blksize_t st_blksize; /* blocksize for file system I/O */  long int 
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */long int
               time_t    st_atime;   /* time of last access */       (*最后访问的时间)long unsigned int
               time_t    st_mtime;   /* time of last modification */ (*最后修改的时间)
               time_t    st_ctime;   /* time of last status change */(*最后的状态改变的时间)
           };

5. 实现`ls`命令功能

- **文件类型和权限**:通过`st_mode`字段判断文件类型和权限。

       The following flags are defined for the st_mode field:

           S_IFMT     0170000   bit mask for the file type bit fields
           S_IFSOCK   0140000   socket
           S_IFLNK    0120000   symbolic link
           S_IFREG    0100000   regular file
           S_IFBLK    0060000   block device
           S_IFDIR    0040000   directory
           S_IFCHR    0020000   character device
           S_IFIFO    0010000   FIFO
		    //---------------------------------------------------------------
           S_ISUID    0004000   set UID bit
           S_ISGID    0002000   set-group-ID bit (see below)
           S_ISVTX    0001000   sticky bit (see below)
		   //---------------------------------------------------------------
           S_IRWXU    00700     mask for file owner permissions
           S_IRUSR    00400     owner has read permission
           S_IWUSR    00200     owner has write permission
           S_IXUSR    00100     owner has execute permission
		    //---------------------------------------------------------------
           S_IRWXG    00070     mask for group permissions
           S_IRGRP    00040     group has read permission
           S_IWGRP    00020     group has write permission
           S_IXGRP    00010     group has execute permission
		    //---------------------------------------------------------------
           S_IRWXO    00007     mask for permissions for others (not in group)
           S_IROTH    00004     others have read permission
           S_IWOTH    00002     others have write permission
           S_IXOTH    00001     others have execute permission

- **硬链接数**:通过`st_nlink`字段获取硬链接数。

- **用户名和组名**:通过`st_uid`和`st_gid`字段获取文件所有者和组名。

- **文件大小**:通过`st_size`字段获取文件大小。

- **最后修改时间**:通过`st_mtime`字段获取最后修改时间,并使用`localtime`或`ctime`函数转换为可读格式。

6. Linux下时间获取

- **time**:获取1970年到现在的秒数。

tm = time(NULL);
功能:
    获得1970年到现在的秒数
参数:
    t:存放秒数的空间首地址
返回值:
    成功返回1970年到现在的秒数
    失败返回-1

- **localtime**:将秒数转换为日历时间。

struct tm *localtime(const time_t *timep);
功能:
    将一个秒数转化成日历时间
参数:
    timep:保存秒数空间的地址
返回值:
    成功返回保存日历时间结构体的指针
    失败返回NULL

- **ctime**:将秒数转换为日期字符串。

char *ctime(const time_t *timep);//date
功能:
    将时间秒数转化成字符串
参数:
    timep:保存时间空间的地址
返回值:
    成功返回获得时间字符串的首地址
    失败返回NULL

struct tm 
 {
int tm_sec;         /* seconds */
int tm_min;         /* minutes */
int tm_hour;        /* hours */
int tm_mday;        /* day of the month */
int tm_mon;         /* month */
int tm_year;        /* year */
int tm_wday;        /* day of the week */
int tm_yday;        /* day in the year */
int tm_isdst;       /* daylight saving time */
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值