使用文件IO,查询目录中的文件

open函数:

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

头文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

功能:使用文件IO的方式打开文件

参数:

 pathname:想要打开的文件的路径及名字
 flags:打开的标志
         O_RDONLY :只读方式打开文件
         O_WRONLY :只写方式打开文件
         O_RDWR :读写方式打开文件
         O_APPEND :以追加方式打开文件
         O_CREAT :创建文件,如果使用了O_CREAT就必须填写第三个mode参数
         O_TRUNC :清空文件
         O_EXCL :和O_CREAT结合使用,如果文件不存在就创建,

        如果文件存在就返回错误码EEXIST
返回值:成功返回文件描述符fd (fd一般都是大于等于3)
        失败返回-1置位错误码

opendir函数

DIR *opendir(const char *name);

头文件

#include <sys/types.h>
#include <dirent.h>

功能: 打开目录文件
参数:
            name:目录的字符串
返回值:成功返回目录的指针,
            失败返回NULL置位错误码

stat函数

int stat(const char *pathname, struct stat *statbuf);

头文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


功能:获取文件的属性信息
参数:
    pathname:路径及名字
    statbuf:属性信息结构体的地址
           struct stat {
               dev_t     st_dev;         //磁盘的设备号
               ino_t     st_ino;        //inode号,文件系统识别文件的唯一标号
               mode_t    st_mode;       //文件的类型和权限
                   S_IFMT     0170000   //文件类型的掩码位
                   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           //管道文件
               nlink_t   st_nlink;       //硬链接数,文件别名的个数
               uid_t     st_uid;         //用户的id
               gid_t     st_gid;         //组的id
               dev_t     st_rdev;        //如果是字符或者块设备文件,这里表示的是它的设备号
               off_t     st_size;        //文件大小,单位是字节
               blksize_t st_blksize;     //块的大小,一般是512字节
               blkcnt_t  st_blocks;      //文件大小,单位是块
               struct timespec st_atim;  //最后一次访问这个文件的时间
               struct timespec st_mtim;  //最后一次修改文件的时间
               struct timespec st_ctim;  //最后一次状态改变的时间
           };
返回值:成功返回0,失败返回-1置位错误码

close函数

int close(int fd);

头文件:

#include <unistd.h>


功能:关闭文件
参数:
    @fd:文件描述符
返回值:成功返回0,失败返回-1置位错误码

closedir函数

int closedir(DIR *dirp);

头文件:

#include <sys/types.h>

#include <dirent.h>


功能:关闭目录文件
参数:
        dirp:目录指针
返回值:成功返回0,失败返回-1置位错误码

程序完整代码:

#include <dirent.h>
#include <head.h>
int main(int argc, const char* argv[])
{
    DIR* dr;
    int fd;
    struct dirent* dt;
    struct stat st;
    int index = -1;
    if ((dr = opendir(argv[1])) == NULL)
        PRINT_ERR("open dir error");
    if ((fd = open(argv[2], O_RDWR)) == -1) {
        PRINT_ERR("文件不存在!");
        return -1;
    }
    if (stat(argv[2], &st))
        PRINT_ERR("get file stat error");
    while ((dt = readdir(dr)) != NULL) {
        if (dt->d_ino != st.st_ino) {
            continue;
        }
        index = 1;
        switch (dt->d_type) {
        case DT_BLK:
            printf("这是块设备文件\n");
            break;
        case DT_CHR:
            printf("这是字符设备文件\n");
            break;
        case DT_DIR:
            printf("这是目录文件\n");
            break;
        case DT_FIFO:
            printf("这是管道文件\n");
            break;
        case DT_LNK:
            printf("这是软连接文件\n");
            break;
        case DT_REG:
            printf("这是普通文件\n");
            break;
        case DT_SOCK:
            printf("这是套接字文件\n");
            break;
        case DT_UNKNOWN:
            printf("未知的文件类型\n");
            break;
        }
        printf("name=%s,ino=%ld,size=%d,type=%d\n",
            dt->d_name, dt->d_ino, dt->d_reclen, dt->d_type);
        break;
    }
    if (index == -1) {
        puts("这个文件不存在!");
    }
    close(fd)
    closedir(dr);

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值