嵌入式学习——文件IO(目录文件)——day22

1. 标准IO和文件IO的区别

    1.标准IO是库函数,是针对于系统调用的封装
    2.文件IO是系统调用,是Linux内核中的函数接口
    3.标准IO是有缓存的,文件是没有缓存

2. 文件流指针和对应文件描述符的相互转化

        1.fileno 

        获得文件流指针对应的文件描述符

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    FILE* fp = fopen("./1.txt", "r");
    if(NULL == fp)
    {
        printf("fopen error!\n");
        return -1;
    }

    int fd = fileno(fp);
    char buf[10]={0};
    read(fd,buf,sizeof(buf));
    printf("%s\n",buf);

    fclose(fp);

    return 0;
}

        2.fdopen

        根据文件描述符创建一个文件流指针

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(void)
{
    int fd  = open("./1.txt",O_RDONLY);

    if(-1 == fd)
    {
        return 1;
    }

    FILE* fp = fdopen(fd,"r");

    char buf[10]={0};
    fgets(buf,sizeof(buf),fp);
    printf("%s\n",buf);

    fclose(fp);
    return 0;
}

3. 目录文件IO

3.1 opendir

        1. 定义

                DIR *opendir(const char *name); 

        2. 功能

                打开一个目录,获得目录流指针

        3. 参数

                name:目录路径

        4. 返回值

                成功返回目录流指针
                失败返回NULL

        5. 示例程序

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

int main(void)
{
    DIR *dir = opendir("./");  
 
    if(NULL == dir)
    {
        printf("opendir");
        return 1;
    }

    return 0;
}

3.2  closedir

        1. 定义

                int closedir(DIR *dirp); 

        2. 功能

              关闭目录流指针

        3. 参数

                dirp:目录流指针

        4. 返回值

                成功返回0 
                失败返回-1  

        5. 示例程序

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

int main(void)
{
    DIR *dir = opendir("./");  
 
    if(NULL == dir)
    {
        printf("opendir");
        return 1;
    }

    closedir(dir);

    return 0;
}

3.3  readdir

        1. 定义

                struct dirent *readdir(DIR *dirp);

            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 supporte by all filesystem types */
                       char           d_name[256]; /* Null-terminated filename */
            };

        2. 功能

              从目录流中读取一个目录项

        3. 参数

                dirp:目录流指针

        4. 返回值

                成功返回目录项信息的结构体指针
                失败返回NULL
                读到末尾返回NULL

        5. 示例程序

                1. 基本用法

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

int main(void)
{
    DIR* dir = opendir("./");   

    if(NULL == dir)
    {
        printf("opendir error!\n");
        return -1;
    }

    struct dirent *info =  readdir(dir);

    printf("%s  %lu",info->d_name,info->d_ino);
    if( DT_DIR ==info->d_type )
    {
        printf("dir\n");
    }
    else if(DT_REG == info->d_type )
    {
        printf("普通\n");
    }
    else 
    {
        printf("其他\n");
    }

    return 0;
}

        2. 实现ls功能

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

int main(void)
{
    DIR* dir = opendir("./");   

    if(NULL == dir)
    {
        printf("opendir error!\n");
        return -1;
    }

    while(1)
    {
        struct dirent *info =  readdir(dir);

        if(NULL == info)
        {
            break;
        }

        printf("%s  %lu",info->d_name,info->d_ino);
        if( DT_DIR ==info->d_type )
        {
            printf("dir\n");
        }
        else if(DT_REG == info->d_type )
        {
            printf("普通\n");
        }
        else 
        {
            printf("其他\n");
        }

    }

    closedir(dir);

    return 0;
}

3.4  chdir

        1. 定义

                int chdir(const char *path);

        2. 功能

              切换当前任务的工作路径

        3. 参数

                path:工作路径

        4. 返回值

                成功返回0 
                失败返回-1 

        5. 示例程序

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int ret = chdir("../");

    if(-1 == ret )
    {
        printf("chdir error!\n");
        return -1;
    }

    fopen("aaa","w");

    return 0;
}

3.5  getcwd

        1. 定义

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

        2. 功能

              获得当前所在目录的绝对路径

        3. 参数

                buf:存放路径字符串空间首地址
                size:最多存放字符串的个数 

        4. 返回值

                成功返回存放字符串首地址的指针
                失败返回NULL

        5. 示例程序

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    char buf[256] = {0};
    char *p = NULL;

    p = getcwd(buf, sizeof(buf));
    if (NULL == p)
    {
        printf("getcwd error!\n");
        return -1;
    }

    printf("%s\n", buf);

    return 0;
}

3.6  mkdir

        1. 定义

               int mkdir(const char *pathname, mode_t mode);

        2. 功能

              创建目录pathname

        3. 参数

                pathname:目录路径 
                mode:权限

        4. 返回值

                成功返回0 
                失败返回-1  
        
                r:是否能够查看目录中文件信息
                w:是否能在文件夹中创建文件
                x:是否能够进入目录中

        5. 示例程序

#include <stdio.h>
#include <sys/stat.h>

int main(void)
{
    int ret = mkdir("666", 0777);

    if (-1 == ret)
    {
        printf("mkdir error!\n");
        return -1;
    }

    return 0;
}

3.7  rmdir

        1. 定义

               int rmdir(const char *pathname);

        2. 功能

              删除目录文件 

        3. 参数

                pathname:目录文件的路径

        4. 返回值

                成功返回0 
                失败返回-1 

        5. 示例程序

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int ret = rmdir("666");

    if (-1 == ret)
    {
        printf("rmdir error!\n");
        return -1;
    }

    return 0;
}

4. 文件属性获取

4.1 stat

        1. 定义

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

        2. 功能

              获得文件的详细信息

        3. 参数

                pathname:文件的路径 
                statbuf:存放文件信息空间首地址

        4. 返回值

                成功返回0 
                失败返回-1 

    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
    };

        5. 示例程序

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

int main(void)
{
    struct stat st;
    int ret = 0;

    memset(&st, 0, sizeof(st));
    ret = stat("./demo_04_ls.c", &st);
    if (-1 == ret)
    {
        printf("stat error!\n");
        return -1;
    }
    
    printf("st_ino: %lu\n", st.st_ino);

    printf("st_mode: ");
    if (S_ISREG(st.st_mode))
    {
        printf("普通!\n");
    }
    else if (S_ISDIR(st.st_mode))
    {
        printf("目录!\n");
    }else
    {
        printf("其他!\n");
    }

    printf("st_nlink: %lu\n", st.st_nlink);
    printf("st_uid: %d\n", st.st_uid);
    printf("st_gid: %d\n", st.st_gid);
    printf("st_size: %lu\n", st.st_size);
    printf("st_mtime: %lu\n", st.st_mtime);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值