C 庫(kù)函數(shù) - perror()
C 標(biāo)準(zhǔn)庫(kù) - <stdio.h>
描述
C 庫(kù)函數(shù) void perror(const char *str) 把一個(gè)描述性錯(cuò)誤消息輸出到標(biāo)準(zhǔn)錯(cuò)誤 stderr。首先輸出字符串 str,后跟一個(gè)冒號(hào),然后是一個(gè)空格。
聲明
下面是 perror() 函數(shù)的聲明。
void perror(const char *str)
參數(shù)
- str -- 這是 C 字符串,包含了一個(gè)自定義消息,將顯示在原本的錯(cuò)誤消息之前。
返回值
該函數(shù)不返回任何值。
實(shí)例
下面的實(shí)例演示了 perror() 函數(shù)的用法。
#include <stdio.h> int main () { FILE *fp; /* 首先重命名文件 */ rename("file.txt", "newfile.txt"); /* 現(xiàn)在讓我們嘗試打開(kāi)相同的文件 */ fp = fopen("file.txt", "r"); if( fp == NULL ) { perror("Error: "); return(-1); } fclose(fp); return(0); }
讓我們編譯并運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果,因?yàn)槲覀儑L試打開(kāi)一個(gè)不存在的文件:
Error: : No such file or directory
更多建議: