zoukankan      html  css  js  c++  java
  • strerror 函数

    收藏
    75

    strerror编辑

    本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!
    通过标准错误的标号,获得错误的描述字符串 ,将单纯的错误标号转为字符串描述,方便用户查找错误。
    外文名
    strerror
    语    言
    C语言
    参    数
    错误标号(即error)
    返回值
    描述字符串(char *)

    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 ",errno);
    message=strerror(errno);
    printf("Mesg:%s ",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 ",
    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 " );
    _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的使用

     
    转自 http://www.douban.com/note/165931644/
  • 相关阅读:
    iOS7 iOS8 毛玻璃效果的分别实现
    关于app transfer之后的开发
    微信登录后,请求用户信息时, 返回地址信息是拼音的解决方案
    获得设备型号(含iPhone6 , iPhone 6+)
    iOS 开发者计划申请 2014 年最新心得[转]
    Facebook的Pop动画库相关资料
    iOS添加自定义字体方法
    iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional
    iOS开发 .framework的Optional(弱引用)和Required(强引用)区别, 有错误 Library not found………………
    python接口自动化测试二:常用操作
  • 原文地址:https://www.cnblogs.com/IceSword-syy/p/4261834.html
Copyright © 2011-2022 走看看