linux系统函数学习_(2)read函数和write函数及errno

本文深入探讨了Linux系统中的read和write函数,包括它们的功能、参数、返回值以及在处理非阻塞I/O时可能出现的情况。同时,介绍了errno这个全局变量在系统错误处理中的作用,以及perror()函数如何帮助打印错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux系统函数学习_(2)read函数和write函数及errno

read()函数

功能:从打开的设备或文件中读取数据。
函数原型:

ssize_t read(int fd, void* buf, size_t count);

参数:

  1. fd: 文件描述符
  2. buf: 存数据的缓冲区
  3. count: 缓冲区大小,即请求读取的字节数

返回值:

0: 文件读取完毕,即读到文件末尾
>0: 读到的字节数
-1: 失败,设置errno 并且errno=EAGIN或者EWOULDBLOCK,说明不是read失败,而是read在以非阻塞方式读一个设备文件(网络文件),并且文件无数据。

write()函数

功能:向打开的设备或文件中写数据。
函数原型:

ssize_t write(int fd, const void* buf, size_t count);

参数:

  1. fd: 文件描述符
  2. buf: 待写出数据的缓冲区
  3. count: 数据大小

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

示例:

// write.c
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
	int fd;
	char buf[]="hello,open&write";

    fd = open("test.txt",O_RDWR);
    if(fd == -1)
    {
    	printf("open file error\n");
        exit(1);
    }
    printf("---open ok---\n");

    int res = write(fd,buf,sizeof(buf));
    if(res == -1)
    {
         printf("write file error\n");
         exit(1);
    }
    else
    {
         printf("write file length: %d",res);
    }
    close(fd);
	return 0;
}                                        

结果:在这里插入图片描述
在这里插入图片描述

// read.c
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
    int fd;
    char buf[64];
    
    fd = open("test.txt",O_RDWR);
    if(fd == -1)
    {
        printf("open file error\n");
        exit(1);
    }
    printf("---open ok---\n");

    int res = read(fd,buf,sizeof(buf));
    if(res == -1)
    {
         printf("read file error\n");
         exit(1);
    }
    else
    {
         puts(buf);
    }
    close(fd);
        return 0;
}

结果:
在这里插入图片描述

errno与perror()函数

errno: Linux中系统调用的错误都存储于errno中,errno由操作系统维护,是记录系统的最后一次错误代码,代码是一个int型的值。每个errno值对应着以字符串表示的错误类型,当调用”某些“函数出错时,该函数会重新设置errno的值。定义在头文件errno.h中,是一个全局变量,任何标准C库函数都能对其修改。

错误宏定义位置:

第1-34个错误定义: /usr/include/asm-generic/errno-base.h
第35-133个错误定义:/usr/include/asm-generic/errno.h

在这里插入图片描述
perror()函数:

函数原型:

void perror(const char* s);

头文件stdio.h,用来将上一个函数发生错误的原因输出到标准设备(stderr),参数s所指的字符串会先打印出,后面再加上错误原因字符串,此错误原因依照全局变量errno的值来决定要输出的字符串。

示例:

// read_write.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
	// 打开一个已经存在的文件
	int fd = open("english.txt", O_RDONLY);
	if(fd == -1)
	{
		perror("open");
		exit(1);
	}
	// 创建一个新文件 -- 写操作
	int fd1 = open("newfile", O_CREAT|O_WRONLY, 0664);
	if(fd1 == -1)
	{
		perror("open1");
		exit(1);
	}
	// 读文件
	char buf[2048] = {0};
	int count = read(fd, buf, sizeof(buf));
	if(count == -1)
	{
		perror("read");
		exit(1);
	}
	while(count)
	{
		// 将读出的数据写入到另一个文件中
		int ret = write(fd1, buf, count);
		printf("write bytes %d\n", ret);
		// 继续读文件
		count = read(fd, buf, sizeof(buf));
	}
	// 关闭文件
	close(fd);
	close(fd1);
}

参考

参考黑马程序员linux系统编程资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值