strerror 函数

收藏

75

23

strerror编辑

本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!

通过标准错误的标号,获得错误的描述字符串 ,将单纯的错误标号转为字符串描述,方便用户查找错误。

外文名

strerror

语    言

C语言

参    数

错误标号(即error)

返回值

描述字符串(char *)

目录


1函数名


2函数作用


3头文件


4函数原型


5举例

1函数名编辑

strerror, _strerror, _wcserror, __wcserror

2函数作用编辑

Get a system error message (strerror, _wcserror) or prints a user-supplied error message (_strerror, __wcserror).

获取系统错误信息或打印用户程序错误信息。

3头文件编辑

#include <string.h>

4函数原型编辑


1

2

3

4

char*strerror(interrnum);

char*_strerror(constchar*strErrMsg);

wchar_t*_wcserror(interrnum);

wchar_t*__wcserror(constwchar_t*strErrMsg)

参数:

errnum:错误标号,通常用errno(标准错误号,定义在errno.h中)

Error number.

strErrMsg

User-supplied message.

返回:

指向错误信息指针(即:错误的描述字符串)。

5举例编辑

例一:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include<stdio.h>

#include<string.h>

#include<errno.h>

#include<stdlib.h>

intmain(void)

{

FILE*fp;

externinterrno;

char*message;

if(NULL==(fp=fopen("/dev/dsp","r+")))

{

printf("errno=%d\n",errno);

message=strerror(errno);

printf("Mesg:%s\n",message);

}

exit(0);

}

输出:

error=2

Mesg:No such file or direcory

例二:

// crt_perror.c

// compile with: /W1

/* This program attempts to open a file named

* NOSUCHF.ILE. Because this file probably doesn‘t exist,

* an error message is displayed. The same message is

* created using perror, strerror, and _strerror.

*/

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <io.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <share.h>

int main( void )

{

int fh;

if( _sopen_s( &fh, "NOSUCHF.ILE", _O_RDONLY, _SH_DENYNO, 0 ) != 0 )

{

/* Three ways to create error message: */

perror( "perror says open failed" );

printf( "strerror says open failed: %s\n",

strerror( errno ) ); // C4996

printf( _strerror( "_strerror says open failed" ) ); // C4996

// Note: strerror and _strerror are deprecated; consider

// using strerror_s and _strerror_s instead.

}

else

{

printf( "open succeeded on input file\n" );

_close( fh );

}

}

输出:

perror says open failed: No such file or directory

strerror says open failed: No such file or directory

_strerror says open failed: No such file or directory

转自 : http://baike.baidu.com/link?url=pllZsYxUR2EJ26CRuWn_F8x7cUimLjEi6g_tPsEnENMv3L4zzXhwqtQ1u7ry8IrHbCh29BbgJTVACTPfawZ1J_

linux下错误的捕获:errno和strerror的使用

经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个时候使用errno这个全局变量就相当有用了。
    在程序代码中包含 #include <errno.h>,然后每次程序调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,这样你只需要读errno这个全局变量就可以获得失败原因了。
    例如:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int fd;
extern int errno;

if((fd = open("/dev/dsp",O_WRONLY)) < 0)
{
   printf("errno=%d\n",errno);
}
  
exit(0);
}
如果dsp设备忙的话errno值将是16。

errno.h中定义的错误代码值如下:


看错误代码errno是调试程序的一个重要方法。当linuc C api函数发生异常时,一般会将errno变量(需include
errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因。在实际编程中用这一招解决了不少原本看来莫名其妙的问题。比较
麻烦的是每次都要去linux源代码里面查找错误代码的含义,现在把它贴出来,以后需要查时就来这里看了。
以下来自linux 2.4.20-18的内核代码中的/usr/include/asm/errno.h
#ifndef _I386_ERRNO_H
#define _I386_ERRNO_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 /* Arg 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 */
#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOLCK 37 /* No record locks available */
#define ENOSYS 38 /* Function not implemented */
#define ENOTEMPTY 39 /* Directory not empty */
#define ELOOP 40 /* Too many symbolic links encountered */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#define ENOMSG 42 /* No message of desired type */
#define EIDRM 43 /* Identifier removed */
#define ECHRNG 44 /* Channel number out of range */
#define EL2NSYNC 45 /* Level 2 not synchronized */
#define EL3HLT 46 /* Level 3 halted */
#define EL3RST 47 /* Level 3 reset */
#define ELNRNG 48 /* Link number out of range */
#define EUNATCH 49 /* Protocol driver not attached */
#define ENOCSI 50 /* No CSI structure available */
#define EL2HLT 51 /* Level 2 halted */
#define EBADE 52 /* Invalid exchange */
#define EBADR 53 /* Invalid request descriptor */
#define EXFULL 54 /* Exchange full */
#define ENOANO 55 /* No anode */
#define EBADRQC 56 /* Invalid request code */
#define EBADSLT 57 /* Invalid slot */
#define EDEADLOCK EDEADLK
#define EBFONT 59 /* Bad font file format */
#define ENOSTR 60 /* Device not a stream */
#define ENODATA 61 /* No data available */
#define ETIME 62 /* Timer expired */
#define ENOSR 63 /* Out of streams resources */
#define ENONET 64 /* Machine is not on the network */
#define ENOPKG 65 /* Package not installed */
#define EREMOTE 66 /* Object is remote */
#define ENOLINK 67 /* Link has been severed */
#define EADV 68 /* Advertise error */
#define ESRMNT 69 /* Srmount error */
#define ECOMM 70 /* Communication error on send */
#define EPROTO 71 /* Protocol error */
#define EMULTIHOP 72 /* Multihop attempted */
#define EDOTDOT 73 /* RFS specific error */
#define EBADMSG 74 /* Not a data message */
#define EOVERFLOW 75 /* Value too large for defined data type */
#define ENOTUNIQ 76 /* Name not unique on network */
#define EBADFD 77 /* File descriptor in bad state */
#define EREMCHG 78 /* Remote address changed */
#define ELIBACC 79 /* Can not access a needed shared library */
#define ELIBBAD 80 /* Accessing a corrupted shared library */
#define ELIBSCN 81 /* .lib section in a.out corrupted */
#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
#define ELIBEXEC 83 /* Cannot exec a shared library directly */
#define EILSEQ 84 /* Illegal byte sequence */
#define ERESTART 85 /* Interrupted system call should be restarted */
#define ESTRPIPE 86 /* Streams pipe error */
#define EUSERS 87 /* Too many users */
#define ENOTSOCK 88 /* Socket operation on non-socket */
#define EDESTADDRREQ 89 /* Destination address required */
#define EMSGSIZE 90 /* Message too long */
#define EPROTOTYPE 91 /* Protocol wrong type for socket */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET 102 /* Network dropped connection because of reset */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS 109 /* Too many references: cannot splice */
#define ETIMEDOUT 110 /* Connection timed out */
#define ECONNREFUSED 111 /* Connection refused */
#define EHOSTDOWN 112 /* Host is down */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE 116 /* Stale NFS file handle */
#define EUCLEAN 117 /* Structure needs cleaning */
#define ENOTNAM 118 /* Not a XENIX named type file */
#define ENAVAIL 119 /* No XENIX semaphores available */
#define EISNAM 120 /* Is a named type file */
#define EREMOTEIO 121 /* Remote I/O error */
#define EDQUOT 122 /* Quota exceeded */
#define ENOMEDIUM 123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#endif

同时也可以使用strerror()来自己翻译
如:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int fd;
extern int errno;

if((fd = open("/dev/dsp",O_WRONLY)) < 0)
{
   printf("errno=%d\n",errno);
char * mesg = strerror(errno);
   printf("Mesg:%s\n",mesg);
}
  
exit(0);
}
dsp设备忙的话将输出如下:
errno=16
Mesg:Device or resource busy

转自 http://www.douban.com/note/165931644/

时间: 2024-08-01 12:25:37

strerror 函数的相关文章

Linux/Unix C编程之的perror函数,strerror函数,errno

#include <stdio.h> // void perror(const char *msg); #include <string.h> // char *strerror(int errnum); #include <errno.h> //errno ? errno 是错误代码,在 errno.h头文件中: perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符): strerror?是通过参数 errnum (就是errno)

对于linux下system()函数的深度理解(整理)

对于linux下system()函数的深度理解(整理) (2013-02-07 08:58:54) 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为是其他的代码影响到这个,或是内核驱动文件系统什么的异常导致,昨天有出现了这个问题,就随手百了一下度,问题出现了,很多人都说system()函数要慎用要少用要能不用则不用,system()函数不稳定?

关于linux下system()函数的总结

导读 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.这里必须要搞懂system()函数,因为有时你不得不面对它. 先来看一下system()函数的简单介绍: #include int system(const char *command) system()函数调用/bin/sh来执行参数指定的命令,/bin/sh 一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令: 在该comman

【C/C++】Linux下使用system()函数一定要谨慎

http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原因如何定位,这才是重点.当初因为这个函数风险较多,故抛弃不用,改用其他的方法.这里先不说我用了什么方法,这里必须要搞懂system()函数,因为还是有很多人用了system()函数,有时你不得不面对它. 先来看一下syst

system函数遇到的问题 - 程序死掉

system函数遇到的问题 解决方案见最下边 http://blog.csdn.net/yangzhenzhen/article/details/51505176 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为是其他的代码影响到这个,或是内核驱动文件系统什么的异常导致,昨天有出现了这个问题,就随手百了一下度,问题出现了,很多人都说system(

Linux提供两个格式化错误信息的函数

#include “stdio.h” Void perror(__const char *__s); 其中__s是出现错误的地方,函数向标准错误输出设备输出如下:s:错误的详细信息. Eg.perror(“fopen”); 输出结果:fopen:No such file or directory. #include “string.h” Char *strerror(int __errnum); __errnum指错误代码,可以直接使用errno.Strerror函数并不直接输出错误信息,而是返

文件IO之——阻塞和非阻塞及perror函数

读常规文件是不会阻塞的,不管读多少字节,read一定会在有限的时间内返回.从终端设备或网络读则不一定,如果从终端输入的数据没有换行符,调用read读终端设备就会阻塞,如果网络上没有接收到数据包,调用read从网络读就会阻塞,至于会阻塞多长时间也是不确定的,如果一直没有数据到达就一直阻塞在那里.同样,写常规文件是不会阻塞的,而向终端设备或网络写则不一定.现在明确一下阻塞(Block)这个概念.当进程调用一个阻塞的系统函数时,该进程被置于睡眠(Sleep)状态,这时内核调度其它进程运行,直到该进程等

出错处理函数

当调用系统调用时,若系统调用出错,则系统会修改errno变量的值,errno变量为一个int型的全局变量,在头文件/usr/include/errno.h中被引用性声明,估计定义是在库文件中.当出现不同类型的错误时,会给errno变量赋予不同的值,这些不同的值在errno.h中被定义了. Linux系统提供两个出错处理函数,perror和strerror函数.这两个函数必须在系统调用会影响errno的值的前提下才能使用. perror(const char *s)函数的功能就是根据不同errno

《APUE》中的函数整理

第1章 unix基础知识 1. char *strerror(int errnum) 该函数将errnum(就是errno值)映射为一个出错信息字符串,返回该字符串指针.声明在string.h文件中. 2.void perror(const char *s) 该函数基于当前的errno值,在标准出错文件中输出一条出错消息,然后返回.声明在stdio.h文件中.它首先输出由s指向的字符串,然后是一个冒号,一个空格,接着是errno值对应的出错信息,最后是一个换行符. 第2章 UNIX标准化及实现