admin管理员组

文章数量:1516870

目录


目录操作

函数名:opendir()

头文件:

#include<sys/types.h>

#include<dirent.h>

函数原型:DIR * opendir(const char *name);

函数功能:打开目录

函数参数:打开的目录路径

函数返回值:目录指针

函数使用:

DIR * dirp = opendir("./A");

函数名:closedir()

头文件:

#include<sys/types.h>

#include<dirent.h>

函数原型:int closedir(DIR * dirp);

函数功能:关闭目录

函数参数:目录指针

函数返回值:成功返回0,失败返回-1

函数使用:

closedir(dirp);

函数名:readdir()

头文件:#include <dirent.h>

函数原型:struct dirent *readdir(DIR *dirp);

函数功能:读取dirp的目录信息,以结构体struct dirent保存

 struct dirent {
               ino_t          d_ino;       /* Inode number */
               off_t          d_off;       /* Not an offset; see below */
               unsigned short d_reclen;    /* Length of this record */
               unsigned char  d_type;      /* Type of file; not supported
                                              by all filesystem types */
               char           d_name[256]; /* Null-terminated filename */
           };

其中信息包括:inode编号、目录条目占用空间大小、文件类型、文件名。

函数参数:目录指针

函数返回值:返回结构体指针

函数使用:

DIR * dirp = opendir("./A");
struct dirent *dir = readdir(dirp);
printf("%s",dir->name);

每次读取目录中的一个文件的信息

所有文件都被读取之后,会返回NULL

函数名:mkdir()

头文件:

#include<sys/types.h>

#include<sys/stat.h>

函数原型:int mkdir(const char *pathname, mode_t mode);

函数功能:创建目录,指定权限

函数参数:

pathname:目录及路径

mode:目录的权限设置 真实权限==~umask&mode (常用值0755)

函数返回值:成功返回 0,失败返回-1

函数使用:

mkdir(“./B”, 0755);

函数名:rmdir()

头文件:#include<unistd.h>

函数原型:int rmdir(const char *pathname);

函数功能:删除目录

函数参数:目录及路径

函数返回值:成功返回 0,失败返回-1

函数使用:

 rmdir(“./B”);

函数名:getcwd() ---pwd

头文件:#include<unistd.h>

函数原型:char *getcwd(char *buf, size_t size);

函数功能:获取当前的路径

函数参数:路径保存的地址,缓存大小

函数返回值:路径信息存储的位置

函数使用:

char buf[100] = {0}; 
printf(“%s\n”, getcwd(buf, 100));

函数名:chdir()---cd

头文件:#include<unistd.h>

函数原型:int chdir(const char *path);

函数功能:跳转到指定路径

函数参数:指定的路径

函数返回值:成功返回 0,失败返回-1

函数使用:

 chdir(“../”);

函数名:chmod()

头文件:#include<sys.stat.h>

函数原型:int chmod(const char *pathname, mode_t mode);

函数功能:设置文件权限

函数参数:文件名,【真实权限】 (常用0777)

函数返回值:成功返回 0,失败返回-1

函数使用:

chmod(“./1.txt”,0777);

文件属性

函数名:stat()

头文件:

#include<sys/stat.h>

#include<sys/types.h>

#include<unistd.h>

函数原型:int stat(const char *pathname, struct stat *statbuf);

函数功能:获取 pathname 文件属性保存到 struct stat 结构体中

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */
               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */
           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };

struct stat结构体中包含文件的inode编号、文件的类型和权限模式、硬链接数、uid文件所有者的id、gid文件所有者的组id、特殊文件的设备id(如字符设备文件)、文件总大小、已分配的字节快数量、文件的最后访问时间、最后修改时间。

函数参数:

pathname:文件名

statbuf:属性保存的位置

函数返回值:成功返回 0,失败返回-1 函数使用:

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>
int main(int argc, char *argv[]){ 
    struct stat sb;
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
     if (lstat(argv[1], &sb) == -1) {
         perror("lstat");
         exit(EXIT_FAILURE);
     }
    printf("File type: ");
    switch (sb.st_mode & S_IFMT) {
         case S_IFBLK: printf("block device\n"); break;
         case S_IFCHR: printf("character device\n"); break;
         case S_IFDIR: printf("directory\n"); break;
         case S_IFIFO: printf("FIFO/pipe\n"); break;
         case S_IFLNK: printf("symlink\n"); break;
         case S_IFREG: printf("regular file\n"); break;
         case S_IFSOCK: printf("socket\n"); break;
         default: printf("unknown?\n"); break;
     }
 printf("I-node number: %ld\n", (long) sb.st_ino);
 printf("Mode: %lo (octal)\n", (unsigned long) sb.st_mode);
 printf("Link count: %ld\n", (long) sb.st_nlink);
 printf("Ownership: UID=%ld GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid);
 printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize);
 printf("File size: %lld bytes\n", (long long) sb.st_size);
 printf("Blocks allocated: %lld\n", (long long) sb.st_blocks);
 printf("Last status change: %s", ctime(&sb.st_ctime));
 printf("Last file access: %s", ctime(&sb.st_atime));
 printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
 }

该程序获取指定路径的文件状态信息,并打印文件的类型、inode 编号、模式、链接数、所有者信息、I/O 块大小、文件大小、已分配的块数以及最后状态改变、访问和修改的时间。并使用 switch 语句判断文件类型,并通过 printf 函数输出各种文件属性。

函数名:lstat()

函数原型:int lstat(const char *pathname, struct stat *statbuf);

函数名:fstat()

函数原型:int fstat(int fd, struct stat *statbuf);

函数参数:fd:文件描述符

stat\lstat\fstat 的异同

相同:都是获取文件属性,并保存在 struct stat 类型的结构体中

不同:

  • stat 直接传入文件名,获取的是源文件的属性
  • lstat 直接传入文件名,获取的是链接文件的属性
  • fstat 需要先打开文件,传入 fd,才能获取文件属性

本文标签: 系统函数原型编程