【问题标题】:What is the C++ stream equivalent to perror? [duplicate]与 perror 等效的 C++ 流是什么? [复制]
【发布时间】:2011-08-04 04:41:56
【问题描述】:

可能重复:
C++ alternative to perror()

我找不到与perror 等效的流。有这样的事吗?我喜欢我可以打电话的事实:

perror("Error");

它会填写errno 是什么。我可以对流进行此操作吗?

【问题讨论】:

  • @Erik:是的!...我之前看到过这个功能,但我无法再想出它。谢谢!

标签: c++


【解决方案1】:

打印错误信息:

str << strerror(errno);

如果您谈论的是流错误状态 - 不,您无法获得自动有意义的错误消息。

【讨论】:

    【解决方案2】:

    由于perror 写入stderr,C++ 中的任何等效项都必须完全相同。也就是说,将strerror(errno) 写入流是不够的。流本身应该(我说必须)是标准错误的流。

    下面的代码sn-p/伪代码应该可以给你一个思路:

    // 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2010-11-27
      • 1970-01-01
      • 2021-02-28
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多