【问题标题】:Unable to write date to file无法将日期写入文件
【发布时间】:2016-03-31 20:20:02
【问题描述】:

我想到了一个想法,我想用日期和时间记录我的程序正在做什么。所以我写了一个小函数,编译后没有错误,也没有运行时错误,只是它没有打开log.txt,甚至不会显示日期。

#include <chrono>
#include <ctime>
#include <fstream>
#pragma warning(disable:4996)

void log(const char*& text)
{
    std::fstream fs;
    fs.open("log.txt", std::fstream::in | std::fstream::out | std::fstream::app);

    auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    fs << ctime(&now) << text << std::endl;
}

int main()
{
    const char* log("Testin codenz stuff");
}

【问题讨论】:

  • 我不会同时使用std::fstream::instd::fstream::app
  • 您介意详细说明您的原因吗?
  • @KiloKing 修复关于指针引用 It works just fine 的主要错误。
  • @KiloKing 不要禁用警告。他们在那里是有原因的,除非你用这种语言做真正边缘的事情,否则你应该听他们说什么

标签: c++ c++11 fstream chrono


【解决方案1】:

这一行:

 const char* log("Testin codenz stuff");

正在定义一个名为log 类型为const char * 的局部变量,其值为“Testin codenz stuff”.. 这不是函数调用。只需这样做:

 log("Testin codenz stuff");

【讨论】:

  • 谢谢,虽然从技术上讲log("Testin codenz stuff"); 行不正确,但需要传入const char* 参数。
  • 去掉函数签名中的&amp;
猜你喜欢
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多