【问题标题】:Problems with std::put_time()std::put_time() 的问题
【发布时间】:2013-03-15 19:33:49
【问题描述】:

您好,我在这里有一个非常简单的测试用例,可以在 Visual Studio 2012 下编译。但是它会产生运行时失败。复制产生此故障的行与 cppreference.com 上的许多与时间功能相关的示例完全相同。 带有示例的页面就像这样http://en.cppreference.com/w/cpp/chrono/c/localtime

#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
#include <chrono>
#include <string>

using namespace std;

ofstream & GetTimeStr(ofstream & ofs)
    {
    time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    // fails on this line, very deep inside the runtime code.
    ofs << std::put_time(std::localtime(&rawTime), "%c %Z");
    return ofs;
    }

int main()
    {
    std::ofstream ofs;
    ofs.open("Logger.txt");

    if (ofs.good())
        {
        ofs << "some text " << GetTimeStr(ofs) << " more text ";   
        }
    }

为了保持这篇文章干净,我把堆栈跟踪放在这里 http://ideone.com/WaeQcf

【问题讨论】:

  • 您能否提供有关它如何失败的更多详细信息,例如错误消息、异常、堆栈跟踪?
  • 该错误是一个弹出窗口,提示 Microsoft Visual Studio C 运行时库在 ConsoleApplication6.exe 中检测到致命错误。按 Break 调试程序或按 Continue 终止程序。
  • 在帖子中添加了指向堆栈跟踪的链接。

标签: c++ visual-c++ c++11 chrono


【解决方案1】:

我猜这是 VC 运行时中的一个错误,它是由在 strftime 中使用 %Z 触发的(由 std::put_time 调用):

http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code

不幸的是,它看起来并不是 Microsoft 的高优先级错误。

【讨论】:

  • 好的,谢谢,不用 %Z 也可以,我的项目不用区域信息也可以。
猜你喜欢
  • 1970-01-01
  • 2013-06-22
  • 2012-12-17
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多