C語言庫函數(shù) void perror(const char *str) 描述性錯(cuò)誤信息打印到stderr。首先打印字符串str接著一個(gè)冒號(hào),然后是一個(gè)空格。
以下是聲明perror() 函數(shù)。
void perror(const char *str)
str -- 這是C字符串,其中包含要打印的自定義消息之前的錯(cuò)誤消息本身。
這個(gè)函數(shù)不返回任何值。
下面的例子演示了如何使用perror() 函數(shù)。
#include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp == NULL ) { perror("Error: "); return(-1); } fclose(fp); return(0); }
讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果,因?yàn)槲覀冋噲D打開一個(gè)文件不存在:
Error: : No such file or directory