57.【C语言】字符函数和字符串函数(strerror函数)

目录

11.strerror函数

*简单使用

*错误码打印

*实际的用法

*附:VS中errno.h对错误码的分类


11.strerror函数

*简单使用

strerror string error

cpuscplus的介绍 点我跳转

翻译:

函数

strerror


char * strerror ( int errnum );

得到指向错误信息字符串(简称错误码)的指针

解释errnum的值,产生一条描述错误情况的信息的字符串,就像被库函数设置为errno一样

这个返回的指针指向一个静态的已分配的且不能被程序修改的字符串,进一步调用此函数可能会覆盖内容(不需要特定的库的实施,避免数据竞争)

strerror产生的错误信息的字符串可能特定于每个系统和库实现

参数


errnum:错误码

返回值


一个指向用于描述错误码的错误信息字符串的指针

 简明扼要:strerror的作用:把错误码翻译成错误信息

*错误码打印

for循环打印错误码:

#define   _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
	for (int i=0;i<141;i++)
	{
		printf("%d.%s\n",i,strerror(i));
	}
	return 0;
}

错误码35,37,错误码43~错误码99,错误码131都显示未知错误(Unknown Error)

*实际的用法

编译以下代码,将exe文件放入没有a.txt的目录下运行

#define   _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
	FILE* pf = fopen("a.txt", "r");
	if (pf == NULL)
	{
		printf("文件打开失败,原因是:%s", strerror(errno));
		return 1;//错误返回
	}
	fclose(pf);
	pf = NULL;
	return 0;
}



可见,strerror用来帮助查bug

其实用perror更简单

printf("文件打开失败,原因是:%s", strerror(errno));

等价于

perror("文件打开失败,原因是:");

perror==printf+strerror

附:点我查看cplusplus对perror的介绍

*附:VS中errno.h对错误码的分类

// Error codes
#define EPERM           1
#define ENOENT          2
#define ESRCH           3
#define EINTR           4
#define EIO             5
#define ENXIO           6
#define E2BIG           7
#define ENOEXEC         8
#define EBADF           9
#define ECHILD          10
#define EAGAIN          11
#define ENOMEM          12
#define EACCES          13
#define EFAULT          14
#define EBUSY           16
#define EEXIST          17
#define EXDEV           18
#define ENODEV          19
#define ENOTDIR         20
#define EISDIR          21
#define ENFILE          23
#define EMFILE          24
#define ENOTTY          25
#define EFBIG           27
#define ENOSPC          28
#define ESPIPE          29
#define EROFS           30
#define EMLINK          31
#define EPIPE           32
#define EDOM            33
#define EDEADLK         36
#define ENAMETOOLONG    38
#define ENOLCK          39
#define ENOSYS          40
#define ENOTEMPTY       41

// Error codes used in the Secure CRT functions
#ifndef RC_INVOKED
    #define _SECURECRT_ERRCODE_VALUES_DEFINED
    #define EINVAL          22
    #define ERANGE          34
    #define EILSEQ          42
    #define STRUNCATE       80
#endif

// Support EDEADLOCK for compatibility with older Microsoft C versions
#define EDEADLOCK       EDEADLK

// POSIX Supplement
#ifndef _CRT_NO_POSIX_ERROR_CODES
    #define EADDRINUSE      100
    #define EADDRNOTAVAIL   101
    #define EAFNOSUPPORT    102
    #define EALREADY        103
    #define EBADMSG         104
    #define ECANCELED       105
    #define ECONNABORTED    106
    #define ECONNREFUSED    107
    #define ECONNRESET      108
    #define EDESTADDRREQ    109
    #define EHOSTUNREACH    110
    #define EIDRM           111
    #define EINPROGRESS     112
    #define EISCONN         113
    #define ELOOP           114
    #define EMSGSIZE        115
    #define ENETDOWN        116
    #define ENETRESET       117
    #define ENETUNREACH     118
    #define ENOBUFS         119
    #define ENODATA         120
    #define ENOLINK         121
    #define ENOMSG          122
    #define ENOPROTOOPT     123
    #define ENOSR           124
    #define ENOSTR          125
    #define ENOTCONN        126
    #define ENOTRECOVERABLE 127
    #define ENOTSOCK        128
    #define ENOTSUP         129
    #define EOPNOTSUPP      130
    #define EOTHER          131
    #define EOVERFLOW       132
    #define EOWNERDEAD      133
    #define EPROTO          134
    #define EPROTONOSUPPORT 135
    #define EPROTOTYPE      136
    #define ETIME           137
    #define ETIMEDOUT       138
    #define ETXTBSY         139
    #define EWOULDBLOCK     140
#endif // _CRT_NO_POSIX_ERROR_CODES
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值