トップ 言語 C・C++ C言語 標準ライブラリ errnoに対応するエラーメッセージを取得(strerror関数)

errnoに対応するエラーメッセージを取得(strerror関数)―C言語の標準ライブラリ

C言語の標準ライブラリのstrerror関数を使用して、「errnoに対応するエラーメッセージの取得」する方法をまとめています。複数環境での取得結果もあります。

▲記事トップへ

errnoに対応するエラーメッセージを取得

errnoは、直近に発生したエラーの番号を保持する用途で使用されるint型の変数です。

errno.hにはグローバル変数のように使用できるerrnoマクロが宣言されています。 errnoに対応するエラーは、環境によって異なりますので注意が必要です。

strerror関数を使用することでerrnoに対応するエラーメッセージが取得できます。 strerror関数でエラーメッセージを取得して、メッセージ出力すると環境差が吸収できます。

strerror関数

strerror関数は、C言語の標準ライブラリ(string.h)にあります。strerror関数のプロトタイプ宣言は以下です。

char *strerror (int __errnum);

strerror関数の引数int型でerrnoの番号を指定して呼び出すとエラーメッセージが取得できます。

サンプルコード(sample.c)

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void) {
    int i;
    for(i = 0; i < 132; i++ ) {
        errno = i;
        printf("%03d : %s\n", errno, strerror(errno));
    }

    return 0;
}

Linux環境の例

Linux上にインストールしたgccを使用して動作確認する例を示します。

環境によって、エラー番号とメッセージが異なる場合があります。 本節の下にWindows環境の例でBoralnd C++ コンパイラーの例も示しています。

コンパイル

$ gcc -o sample sample.c
$ ls
sample  sample.c

実行

実行すると0(Success)~132(Unknown error)までのエラーメッセージが表示されます。

$ ./sample
000 : Success
001 : Operation not permitted
002 : No such file or directory
003 : No such process
004 : Interrupted system call
005 : Input/output error
006 : No such device or address
007 : Argument list too long
008 : Exec format error
009 : Bad file descriptor
010 : No child processes
011 : Resource temporarily unavailable
012 : Cannot allocate memory
013 : Permission denied
014 : Bad address
015 : Block device required
016 : Device or resource busy
017 : File exists
018 : Invalid cross-device link
019 : No such device
020 : Not a directory
021 : Is a directory
022 : Invalid argument
023 : Too many open files in system
024 : Too many open files
025 : Inappropriate ioctl for device
026 : Text file busy
027 : File too large
028 : No space left on device
029 : Illegal seek
030 : Read-only file system
031 : Too many links
032 : Broken pipe
033 : Numerical argument out of domain
034 : Numerical result out of range
035 : Resource deadlock avoided
036 : File name too long
037 : No locks available
038 : Function not implemented
039 : Directory not empty
040 : Too many levels of symbolic links
041 : Unknown error 41
042 : No message of desired type
043 : Identifier removed
044 : Channel number out of range
045 : Level 2 not synchronized
046 : Level 3 halted
047 : Level 3 reset
048 : Link number out of range
049 : Protocol driver not attached
050 : No CSI structure available
051 : Level 2 halted
052 : Invalid exchange
053 : Invalid request descriptor
054 : Exchange full
055 : No anode
056 : Invalid request code
057 : Invalid slot
058 : Unknown error 58
059 : Bad font file format
060 : Device not a stream
061 : No data available
062 : Timer expired
063 : Out of streams resources
064 : Machine is not on the network
065 : Package not installed
066 : Object is remote
067 : Link has been severed
068 : Advertise error
069 : Srmount error
070 : Communication error on send
071 : Protocol error
072 : Multihop attempted
073 : RFS specific error
074 : Bad message
075 : Value too large for defined data type
076 : Name not unique on network
077 : File descriptor in bad state
078 : Remote address changed
079 : Can not access a needed shared library
080 : Accessing a corrupted shared library
081 : .lib section in a.out corrupted
082 : Attempting to link in too many shared libraries
083 : Cannot exec a shared library directly
084 : Invalid or incomplete multibyte or wide character
085 : Interrupted system call should be restarted
086 : Streams pipe error
087 : Too many users
088 : Socket operation on non-socket
089 : Destination address required
090 : Message too long
091 : Protocol wrong type for socket
092 : Protocol not available
093 : Protocol not supported
094 : Socket type not supported
095 : Operation not supported
096 : Protocol family not supported
097 : Address family not supported by protocol
098 : Address already in use
099 : Cannot assign requested address
100 : Network is down
101 : Network is unreachable
102 : Network dropped connection on reset
103 : Software caused connection abort
104 : Connection reset by peer
105 : No buffer space available
106 : Transport endpoint is already connected
107 : Transport endpoint is not connected
108 : Cannot send after transport endpoint shutdown
109 : Too many references: cannot splice
110 : Connection timed out
111 : Connection refused
112 : Host is down
113 : No route to host
114 : Operation already in progress
115 : Operation now in progress
116 : Stale NFS file handle
117 : Structure needs cleaning
118 : Not a XENIX named type file
119 : No XENIX semaphores available
120 : Is a named type file
121 : Remote I/O error
122 : Disk quota exceeded
123 : No medium found
124 : Wrong medium type
125 : Operation canceled
126 : Required key not available
127 : Key has expired
128 : Key has been revoked
129 : Key was rejected by service
130 : Owner died
131 : State not recoverable
132 : Unknown error 132

Windows環境の例

Windows上にインストールしたbcc32(Borland C++コンパイラー)を使用して動作確認する例です。

コンパイル

bcc32コマンドの引数にソースファイルを指定して、実行します。

> bcc32 sample.c

実行

実行してみるとわかりますが、gccの方と結果が違いますね。

> sample
000 : Error 0
001 : Invalid function number
002 : No such file or directory
003 : Path not found
004 : Too many open files
005 : Permission denied
006 : Bad file number
007 : Memory arena trashed
008 : Not enough memory
009 : Invalid memory block address
010 : Invalid environment
011 : Invalid format
012 : Invalid access code
013 : Invalid data
014 : Bad address
015 : No such device
016 : Attempted to remove current directory
017 : Not same device
018 : No more files
019 : Invalid argument
020 : Arg list too big
021 : Exec format error
022 : Cross-device link
023 : Too many open files
024 : No child processes
025 : Inappropriate I/O control operation
026 : Executable file in use
027 : File too large
028 : No space left on device
029 : Illegal seek
030 : Read-only file system
031 : Too many links
032 : Broken pipe
033 : Math argument
034 : Result too large
035 : File already exists
036 : Possible deadlock
037 : Operation not permitted
038 : No such process
039 : Interrupted function call
040 : Input/output error
041 : No such device or address
042 : Resource temporarily unavailable
043 : Block device required
044 : Resource busy
045 : Not a directory
046 : Is a directory
047 :
048 : Directory not empty
049 : Unknown error
:
省略。以降、全部Unknown error
:

もっとプログラミング知識を広げるための参考

戻る

スポンサーリンク

サイト内のページ

言語
C・C++ /HTML /Java /JavaScript /PHP /シェルスクリプト

開発環境
Ant /Burp /Eclipse /Fiddler /gcc /gdb /Git /g++ /JDK /JMeter /JUnit /Teraterm /ZAP

技術・仕様
Ajax /CORBA /Jakarta EE(旧称J2EE、Java EE) /JNI

ライブラリ/Framework/CMS
bootstrap /jQuery /FuelPHP /Lucene /MyBatis /Seasar2 /Spring /Struts /WordPress

Web API
Google Maps

ITインフラOSとミドルウェア
Linux /Windows /シェル
ActiveMQ /Tomcat /MariaDB /MySQL /Nagios /Redis /Solr

ITインフラサーバー
公開Webサーバー

ITインフラネットワーク
プログラミング /構築

ITインフラセキュリティ
公開サーバーのセキュリティ

PC製品
ZOTAC

SI
ホームページの作り方

その他
IT用語 /ITスキル体系

スポンサーリンク

関連サイト内検索ツール

zealseedsおよび関連サイト内のページが検索できます。

IPアドレス確認ツール

あなたのグローバルIPアドレスは以下です。

54.163.200.109

HTMLの表示色確認ツール

パスワード生成ツール

文字数のプルダウンを選択して、取得ボタンを押すと「a~z、A~Z、0~9」の文字を ランダムに組み合わせた文字列が表示されます。

ここに生成されます。

スポンサーリンク