打印错误信息:
str << strerror(errno);
如果您在谈论流错误状态 - 那么不,您无法获得自动的有意义的错误消息。
由于 perror
写入到 stderr,因此C ++中的任何等效项都必须完全相同。也就是说,仅将 strerror(errno)
写入流是不够的。流本身应该(我会说必须)是一个指向标准错误的流。
以下代码片段/伪代码应该可以给您一个想法:
// depending on your compiler, this is all you need to include
#include <iostream>
#include <string.h>
#include <errno.h>
... somewhere in your code...
std::cerr << "Error: " << strerror(errno) << std::endl;