news 2026/5/2 16:05:48

Linux应用编程 - errno、perror()、strerror(errno)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux应用编程 - errno、perror()、strerror(errno)

目录

一、errno、perror()、strerror(errno)

二、实例

1、errno

2、perror()

3、strerror(errno)


一、errno、perror()、strerror(errno)


Linux中,errno用于存储系统调用的错误码。
定义和用途
  1. errno是一个预定义的外部整型(int)变量,通常包含在头文件中;
  2. 当系统调用或库函数发生错误时,它们通常会设置errno以指示发生了哪种错误;
  3. errno的值仅在函数失败时才会被设置,并且会覆盖之前的值;
  4. 通过检查errno的值,程序员可以确定发生了什么错误,并据此编写适当的错误处理代码;
路径:kernel/include/uapi/asm-generic/errno-base.h
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Argument list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM 12 /* Out of memory */ #define EACCES 13 /* Permission denied */ #define EFAULT 14 /* Bad address */ #define ENOTBLK 15 /* Block device required */ #define EBUSY 16 /* Device or resource busy */ #define EEXIST 17 /* File exists */ #define EXDEV 18 /* Cross-device link */ #define ENODEV 19 /* No such device */ #define ENOTDIR 20 /* Not a directory */ #define EISDIR 21 /* Is a directory */ #define EINVAL 22 /* Invalid argument */ #define ENFILE 23 /* File table overflow */ #define EMFILE 24 /* Too many open files */ #define ENOTTY 25 /* Not a typewriter */ #define ETXTBSY 26 /* Text file busy */ #define EFBIG 27 /* File too large */ #define ENOSPC 28 /* No space left on device */ #define ESPIPE 29 /* Illegal seek */ #define EROFS 30 /* Read-only file system */ #define EMLINK 31 /* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* Math result not representable */ #endif

二、实例


1、errno

#include <stdio.h> #include <errno.h> int main() { FILE*fp; fp = fopen("temp","r"); if(fp == NULL) { fprintf(stderr,"fopen() failed! errno = %d\n",errno); exit(1); } puts("OK!"); exit(0); }

运行结果:

fopen() failed! errno = 2

errno = 2 从内核定义可以看到表示没有文件或目录

#define ENOENT 2 /* No such file or directory */

直接使用errno存在一个缺陷,因为errno是一个整型数值,需要查看内核代码才能知道含义。可以使用 perror()、strerror(errno) 打印错误字符串。

2、perror()

#include <stdio.h> #include <errno.h> int main() { FILE*fp; fp = fopen("temp","r"); if(fp == NULL) { //fprintf(stderr,"fopen() failed! errno = %d\n",errno); perror("fopen() failed!"); exit(1); } puts("OK!"); exit(0); }

perror()可以直接输出错误信息,输出结果如下:

fopen() failed!: No such file or directory

3、strerror(errno)

strerror(errno)将errno错误号转化成错误字符串

#include <stdio.h> #include <errno.h> #include <string.h> int main() { FILE*fp; fp = fopen("temp","r"); if(fp == NULL) { //fprintf(stderr,"fopen() failed! errno = %d\n",errno); //perror("fopen() failed!"); fprintf(stderr,"fopen() failed! errno = %s\n",strerror(errno)); exit(1); } puts("OK!"); exit(0); }

运行结果:

fopen() failed! errno = No such file or directory

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 5:56:58

YTSage全面指南:5分钟掌握现代化视频下载神器

YTSage全面指南&#xff1a;5分钟掌握现代化视频下载神器 【免费下载链接】YTSage Modern YouTube downloader with a clean PySide6 interface. Download videos in any quality, extract audio, fetch subtitles (including auto-generated), and view video metadata. Built…

作者头像 李华
网站建设 2026/5/1 8:30:23

VITS语音合成实战指南:从零开始打造你的专属AI语音助手

VITS语音合成实战指南&#xff1a;从零开始打造你的专属AI语音助手 【免费下载链接】vits VITS: Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech 项目地址: https://gitcode.com/gh_mirrors/vi/vits 想要快速拥有一个能…

作者头像 李华
网站建设 2026/5/2 12:48:53

GPT-5.2 全面解读:AI 发展新阶段

2025 年 12 月 11 日&#xff0c;OpenAI 正式发布了 GPT-5.2 系列模型&#xff0c;这是迄今为止该公司针对专业知识工作与复杂任务处理打造的最强模型系列之一。此发布也是 OpenAI 在 AI 激烈竞争格局下的一次重要战略调整&#xff0c;特别是为了应对 Google Gemini 3 等先进模…

作者头像 李华
网站建设 2026/5/1 7:11:48

3000亿参数降维打击:ERNIE 4.5用2Bits量化重构企业AI经济学

3000亿参数降维打击&#xff1a;ERNIE 4.5用2Bits量化重构企业AI经济学 【免费下载链接】ERNIE-4.5-300B-A47B-2Bits-TP4-Paddle 项目地址: https://ai.gitcode.com/hf_mirrors/baidu/ERNIE-4.5-300B-A47B-2Bits-TP4-Paddle 导语 百度ERNIE 4.5系列大模型以3000亿总参…

作者头像 李华
网站建设 2026/4/28 18:38:49

deepseek技术总结

dentify the entity: DeepSeek (月之暗面). Identify the core task: Summarize its core technical advantages in ~1000 characters. Recall/Search information about DeepSeek: What is it known for? What are its stated goals or technical focuses? What it is: A pr…

作者头像 李华
网站建设 2026/5/2 11:38:19

66、Linux系统脚本编写、邮件配置、数据库使用及安全设置指南

Linux系统脚本编写、邮件配置、数据库使用及安全设置指南 1. 环境变量与脚本编写 在Linux系统中,环境变量扮演着重要角色。以下是一些关键环境变量及其作用: - PS1 :该环境变量包含各种格式化代码和文本,用于设置主命令提示符。例如,你可以通过设置PS1让命令提示符显…

作者头像 李华