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

相关推荐
dhxhsgrx2 小时前
PYTHON训练营DAY25
java·开发语言·python
风逸hhh4 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生4 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
chxii6 小时前
5java集合框架
java·开发语言
老衲有点帅6 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈6 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
inputA6 小时前
【LwIP源码学习6】UDP部分源码分析
c语言·stm32·单片机·嵌入式硬件·网络协议·学习·udp
IsPrisoner6 小时前
Go语言安装proto并且使用gRPC服务(2025最新WINDOWS系统)
开发语言·后端·golang
Python私教7 小时前
征服Rust:从零到独立开发的实战进阶
服务器·开发语言·rust