C-standard-library-c-function-perror
提供:Dev Guides
Cライブラリ関数-perror()
説明
Cライブラリ関数 void perror(const char str)は、記述エラーメッセージをstderrに出力します。 最初に文字列 *str が出力され、その後にコロン、スペースが続きます。
宣言
以下は、perror()関数の宣言です。
void perror(const char *str)
パラメーター
- str -これは、エラーメッセージ自体の前に出力されるカスタムメッセージを含むC文字列です。
戻り値
この関数は値を返しません。
例
次の例は、perror()関数の使用法を示しています。
#include <stdio.h>
int main () {
FILE *fp;
/*first rename if there is any file*/
rename("file.txt", "newfile.txt");
/*now let's try to open same file*/
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error: ");
return(-1);
}
fclose(fp);
return(0);
}
存在しないファイルを開こうとしているため、次の結果を生成する上記のプログラムをコンパイルして実行します-
Error: : No such file or directory