linux文件io

Linux中一切皆文件

linux文件系统的头文件:
/usr/src/linux-headers-4.15.0-142-generic/include/linux/fs.h

struct file这个结构体是在文件被操作时会创建的。
struct file_operations这个结构体中的成员几乎都是函数指针,用于做统一化管理。


标准文件IO:
    1、C标准库
    2、具有良好的移植特性
    3、通常在用户态下使用
    4、高级函数,用于应用层
    5、执行效率高
    6、文件操作在用户态和内核态下交互次数少
    7、能够使用标准IO的情况下就不要使用系统IO
系统文件IO:
    1、在内核中
    2、不可移植
    3、通常在内核态下使用
    4、低级函数,用于内核层
    5、执行效率相对较低


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

/*
功能:打开文件
参数1:待打开文件的文件名及其路径
参数2:打开文件的方式
      O_RDONLY  只读
      O_WRONLY  只写
      O_RDWR    读写
      O_APPEND  追加
      O_CREAT   如果文件不存在则创建,存则直接打开
      O_TRUNC   如果文件存在则清空
参数3:只有在文件被创建时才会生效,用于设置被创建的文件的权限。
      如果文件被创建时,没有设置这个参数,则文件访问权限随机。
      如果文件已经存,无法通过这个参数修改文件权限。
      一般使用0644 或者 0777
返回值:成功返回文件描述符,失败返回-1且errno被自动设置
*/
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

从文件中读取数据
/*
功能:从文件中读取数据
参数1:待读取文件的文件描述符
参数2:存储读取的数据
参数3:将要读取的字节数
返回值:成功读取的字节数,失败返回-1且errno被自动设置
*/
ssize_t read(int fd, void *buf, size_t count);


/*
功能:向文件中写入数据
参数1:待写入文件的文件描述符
参数2:待写入的数据
参数3:将要写入的字节数
返回值:成功写入的字节数,失败返回-1且errno被自动设置
*/
ssize_t write(int fd, const void *buf, size_t count);

/*
功能:关闭文件
参数1:待关闭文件的文件描述符
返回值:成功返回0, 失败返回-1且errno被自动设置
*/
int close(int fd);

head.h

#ifndef _HEAD_H
#define _HEAD_H

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>


#endif //_HEAD_H

osfile.c

#include "head.h"

int main(int argc,char *argv[])
{
    // 以只读方式打开文件
    int fd_r = open("osfile.c", O_RDONLY);
    if (fd_r < 0)
    {
        //printf("open file!\n");
        //printf("osfile.c open fail: %s\n", strerror(errno));
        perror("osfile.c open fail");

        return -1;
    }
    else
    {
        //int aa = fcntl(fd_r, F_DUPFD, 100);

        printf("fd_r = %d\n", fd_r);
    }

    int fd_w = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd_w < 0)
    {
        perror("1.txt open fail");
        return -1;
    }
    else
    {
        printf("fd_w = %d\n", fd_w);
    }

    char buf[100];

    while(1)
    {
        //memset(buf, 0, sizeof(buf));
        bzero(buf, sizeof(buf));
        int len = read(fd_r, buf, sizeof(buf)-1);
        if (len <= 0)
        {
            break;
        }

        write(fd_w, buf, len);
    }

    close(fd_r);
    close(fd_w);

    return 0;
}

dirent.c

#include "head.h"

int main(int argc,char *argv[])
{
    DIR* dir = opendir("../XAYQ2407");
    if (dir == NULL)
    {
        perror("opendir error");
        return -1;
    }

    struct dirent* df = NULL;

    while(1)
    {
        df = readdir(dir);
        if (df == NULL)
        {
            break;
        }
        printf("%s\n", df->d_name);
    }

    return 0;
}

stat.c

#include "head.h"

int main(int argc,char *argv[])
{
    struct stat st;

    stat("osfile.c", &st);
    printf("%lu\n", st.st_size);

    printf("%u\n", major(st.st_dev));
    printf("%u\n", minor(st.st_dev));

    if (S_ISREG(st.st_mode))
    {
        printf("普通文件\n");
    }
    else if (S_ISCHR(st.st_mode))
    {
        printf("字符设备文件\n");
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值