C语言中的IO控制流

文章目录

  • 一、什么是C语言中的IO控制流
  • 二、open函数
    • 1.使用open函数创建文件
    • 2.使用使用open函数打开文件
  • 三、文件的权限
  • 四、文件的描述符
  • 五、read函数
  • 六、write函数
  • 七、lseek函数
  • 八、close函数

一、什么是C语言中的IO控制流

在linux系统中一切皆文件,C语言中的IO控制流就是用于操作文件的一组函数。

二、open函数

      open函数常用于打开或创建文件

      函数原型:两个参数:int open(const char *pathname, int flags)

      三个参数:int open(const char *pathname, int flags, mode_t mode)

      返回值:成功返回文件描述符,失败返回-1

      参数1:const char *pathname表示文件袋全名,就是路径+文件名

      参数2:int flags表示打开文件的方式,

      O_RDONLY只读方式打开文件,O_WRONLY只写方式打开文件,O_RDWR读写方式打文件

      O_CREAT文件不存在就创建文件,O_TRUNC文件文件存在清空文件内容

      参数3:mode_t mode表示文件的权限,在前面需要加一个0

  

1.使用open函数创建文件

一般使用三个参数时open用于打开文件

//1.使用open函数创建文件

    int fd = open("./a.txt",O_CREAT,0777);

    if(fd == -1){

        perror("创建失败");

        return -1;

    }

    printf("文件描述符:%d\n", fd);

    close(fd);

2.使用使用open函数打开文件

一般使用两个参数open时用于打开文件

//1.打开文件

    int fd = open("./a.txt",O_RDWR);

    if(fd == -1){

        perror("打开文件失败");

        return -1;

    }

    printf("文件描述符:%d\n", fd);

    close(fd);

三、文件的权限

我们打开linux虚拟机的终端,我们输入命令ls -l列出文件的详细的信息

我们看最左边的第一个字符表示该文件的类型,从第二个字符到第十个字符就是表示该文件的权限

我们以第一个文件bin举例:

权限为三个八进制数,r(可读权限)对应的十进制数为4,w(可写权限)对应的十进制数为2,x(可运行权限)对应的十进制数为1

四、文件的描述符

1.什么是文件描述符

我们可以把文件的描述符当成文件一个标志,它是int类型的整数。当我们使用open函数操作文件时就会返回给我们一个文件描述符,该文件描述符可以代表整个文件

int fd = open("./a.txt",O_RDWR);//fd为a.txt文件的描述符

close(fd);//就是将文件a.txt文件的资源释放,此时fd文件描述符就是代表文件a.txt

2.文件描述符的分配

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

int main(){

    //1.使用open函数创建文件

    int fd = open("./a.txt",O_CREAT,0777);

    if(fd == -1){

        perror("打开失败");

        return -1;

    }

    printf("文件描述符:%d\n", fd);

    close(fd);

}

运行结果:

五、read函数

      read函数

      函数原型:size_t read(int fd, const void *buf, size_t count);

      参数1:int fd:文件的描述符

      参数2:const void *buf:字符缓冲去数组

      参数3:size_t count:读取字符的个数

      返回值:成功返回读取字符的个数,失败返回-1

      功能:将指定文件的内容读入字符缓冲去数组中

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

int main(){

    //1.打开文件

    int fd = open("./a.txt",O_RDWR);

    if(fd == -1){

        perror("打开文件失败");

        return -1;

    }

    printf("文件描述符:%d\n",fd);

    //2.读取文件的内容

    //2.1创建一个字符缓冲区数组

    char buf[1024];

    //2.2调用read函数读取文件内容

    read(fd,buf,sizeof(buf));

    printf("读取的内容:%s\n",buf);

    close(fd);

}

六、write函数

      write函数

      函数原型:size_t write(int fd, const void *buf,size_t count);

      参数1:int fd,文件描述符

      参数2:const void *buf,要写入的数据

      参数3:size_t count,要写入的数据的字节数

      返回值:成功返回写入的字节数,失败返回-1

      功能:将字符缓冲数组中的内容写入文件中

#include<stdio.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<string.h>

#include<fcntl.h>

#include<unistd.h>

int main(){

    //1.打开文件

    int fd = open("./a.txt",O_RDWR);

    //2.创建字符缓冲去数组

    char buf[] = "hello world";

    //3.调用write函数将字符缓冲区中的内容写入文件中

    write(fd,buf,strlen(buf));

    //4.关闭文件

    close(fd);

}

七、lseek函数

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

#include<string.h>

int main(){

     * lseek函数

     * 函数原型:off lseek(int fd, off offset, int whence);

     * 参数1:int fd:文件描述符

     * 参数2:off offset:偏移量

     * 参数3:int whence:偏移起始位置

     * 位置1:SEEK_SET文件的开头

     * 位置2:SEEK_CUR当前位置

     * 位置3:SEEK_END文件的结尾

     * 返回值:成功返回偏移量,失败返回-1

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

#include<string.h>

int main(){

    //1.打开文件

    int fd =  open("./a.txt",O_RDWR);

    //2.设置偏移量

    lseek(fd,11,SEEK_SET);

    //3.读文件

    // //3.1创建一个字符缓冲数组

    // char buf[1024];

    // read(fd,buf,sizeof(buf));

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

    //写入文件

    char buf[1024] = "hello world";

    write(fd,buf,strlen(buf));

   

    //4.关闭文件

    close(fd);

}

八、close函数

        close函数

        函数原型:int close(int fd);

        参数:int fd 文件的描述符

        返回值:int 类型的整数,操作成功返回1,否则返回-1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值