【发布时间】:2013-02-27 00:41:49
【问题描述】:
我正在使用非标准函数warn()(由BSD提供)在无法打开文件时输出错误消息,如下所示:
std::string path = get_path() ;
std::ifstream file(path) ;
if (file.is_open()) { /* do something */ }
else {
warn("%s", path.c_str()) ;
// uses errno to figure out what the error was and outputs it nicely along with the filename
}
这对于输出它来说非常好,但是如果我想在其他地方使用整个字符串,除了打印它怎么办? warn() 函数似乎没有将错误写入字符串的形式。我尝试过自己滚动,但相比之下它似乎非常麻烦(除了没有得到程序的名称):
this->foo((boost::format("%s: %s") % path % strerror(errno)).str()) ;
那么如何将warn() 的输出作为字符串获取?
【问题讨论】:
-
'
warn从何而来?它当然不是一个标准的 c++ 函数,所以你使用它的 API 的文档可能会帮助更多...... -
@PlasmaHH 它是,嗯,一个标准的 C 函数?
-
@BlacklightShining:它,嗯,不是。
-
@BlacklightShining:是的,嗯,不是。
-
@PlasmaHH 它的手册页说它在标准 C 库中……
标签: c++ error-handling bsd