C++ 中的perror() 函數根據當前存儲在係統變量errno 中的錯誤代碼將錯誤消息打印到stderr。
perror()原型
void perror(const char* str);
perror()
函數解釋 errno 的值以將錯誤消息打印到 stderr
。 errno
保存確定錯誤條件的整數值。
錯誤消息是通過連接以下組件形成的:
- str 指向的字符串的內容。
- 冒號,即':'。
- 說明存儲在 errno 中的錯誤代碼的錯誤消息。
- 換行符,即'\n'。
它在<cstdio> 頭文件中定義。
參數:
str
:指向空終止字符串的指針。
返回:
沒有。
示例:perror() 函數的工作原理
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char filename[] = "example.txt";
/* if the file can not be reomved */
if (remove(filename) != 0)
perror("File deletion failed");
else
cout << "File deleted successfully";
return 0;
}
如果文件不存在運行程序將產生:
File deletion failed: No such file or directory
相關用法
- C++ puts()用法及代碼示例
- C++ putc()用法及代碼示例
- C++ pow()用法及代碼示例
- C++ priority_queue top()用法及代碼示例
- C++ putwchar()用法及代碼示例
- C++ printf()用法及代碼示例
- C++ priority_queue pop()用法及代碼示例
- C++ priority_queue::empty()、priority_queue::size()用法及代碼示例
- C++ priority_queue push()用法及代碼示例
- C++ priority_queue value_type用法及代碼示例
- C++ priority_queue size()用法及代碼示例
- C++ priority_queue swap()用法及代碼示例
- C++ priority_queue::top()用法及代碼示例
- C++ priority_queue::push()、priority_queue::pop()用法及代碼示例
- C++ putwc()用法及代碼示例
- C++ priority_queue empty()用法及代碼示例
- C++ priority_queue emplace()用法及代碼示例
- C++ complex polar()用法及代碼示例
- C++ priority_queue::swap()用法及代碼示例
- C++ putchar()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ perror()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。