如何在c++中将名称文件设置为变量

  • 本文关键字:文件 设置 变量 c++ c++
  • 更新时间 :
  • 英文 :


如何设置"dt";变量作为文件名?

time_t now = time(0);
string dt = ctime(&now);

ofstream file;
file.open( ??? );

我正在尝试将dt设置为名称

由于C++11,您可以简单地将string作为参数传递:file.open(dt);

在此之前,您必须传递一个const char*作为参数:file.open(dt.c_str());

https://en.cppreference.com/w/cpp/io/basic_ofstream/open

您也可以直接使用构造函数:C++11之前的ofstream file(dt);ofstream file(dt.c_str());

https://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream

最新更新