C++ 在C+中使用日期和时间命名日志文件+;

C++ 在C+中使用日期和时间命名日志文件+;,c++,C++,因此,我想为我正在尝试创建的应用程序创建一个日志文件,但我不知道如何将日志命名为“log/date&time” 无论如何,这是我的代码: #include <iostream> #include <fstream> #include <time.h> #include <stdio.h> #include <sstream> using namespace std; int main (int argc, char *argv[

因此,我想为我正在尝试创建的应用程序创建一个日志文件,但我不知道如何将日志命名为“log/date&time”

无论如何,这是我的代码:

#include <iostream>
#include <fstream>
#include <time.h>
#include <stdio.h>
#include <sstream>


using namespace std;


int main (int argc, char *argv[])
{

     time_t t = time(0);
     struct tm * now = localtime( & t );

     char buffer [80];
     strftime (buffer,80,"%Y-%m-%d.",now); //i think i can't just put "log/%Y-%m-%d." there.

     ofstream myfile;
     myfile.open ("log/" + buffer); // this is my problem, i can't put the ' "log/" + ' part there
     if(myfile.is_open())
     {
         cout<<"Success"<<std::endl;
      }

return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
时间t=时间(0);
struct tm*now=localtime(&t);
字符缓冲区[80];
strftime(buffer,80,“%Y-%m-%d.”,现在);//我想我不能把“log/%Y-%m-%d.”放在那里。
流文件;
myfile.open(“log/”+buffer);//这是我的问题,我不能把“log/”+部分放在那里
如果(myfile.is_open())
{

cout您应该使用
std::string
,它支持通过重载的
操作符+。

std::string buffer(80, '\0');
strftime( &buffer[0], buffer.size(), "some format string", now);

/* ... */
std::ofstream myfile( ("log/" + buffer).c_str() ); 
// Remove the (..).c_str() part when working with a C++11 conforming
// standard library implementation
你真正的问题是“为什么这不起作用”

回答——因为C++不支持你想要的东西——用char *连接字符串文本,并返回另一个ch**./p> 做


考虑改用std::facilities(想到std::string和std::ostringstream):

std::ostream和time_位(std::ostream和out,无符号整数位)
{//便利功能:为下一个输入应用宽度和填充

return out我在myfile.open部分出错:无效参数的候选参数为:void open(const char*,enum std:_Ios\u Openmode)“@RobotionX8..你确定你复制了1:1的代码吗?对不起,当我读到答案时,没有全部内容。它现在没有给我一个错误,但不会创建任何错误file@roboticonx8但这是一个完全不同的问题。我无法让它工作…:(无论如何,这是我的代码:我写的东西没有错误,但不会写入文件。@RobotionX8,我无法从office访问pastebin。我将在今天下午打开它。
 myfile.open ("log/" + buffer);
std::string filetime(buffer);

std::string filename = "log/" + filetime;

open(filename.c_str());
std::ostream& time_digits(std::ostream& out, unsigned int digits)
{ // convenience function: apply width and fill for the next input
    return out << std::setw(digits) << std::setfill('0');
}

std::string unique_log_name()
{ // generate unique log name, depending on local time
  // example output: "log/2014-04-19.log"

    auto now = time(0);
    tm *ltm = localtime(&now);

    std::ostringstream buffer;
    buffer
        << "log/" << time_digits(4) << ltm.tm_year
        << "-" << time_digits(2) << ltm.tm_mon
        << "-" << time_digits(2) << ltm.tm_day;

    // could also add these to the name format:
    // buffer
    //     << "-" << time_digits(2) << ltm.dm_hour
    //     << "-" << time_digits(2) << ltm.tm_min
    //     << "-" << time_digits(2) << ltm.tm_sec;

    buffer << ".log"; // add extension

    return buffer.str();
}

void client_code()
{ // construct log stream on unique file name
    ofstream myfile{ unique_log_name() };
    if(myfile)
    {
        cout << "Success" << std::endl;
    }
}