C++ 如何命名在当前日期/时间之后创建的文本文件

C++ 如何命名在当前日期/时间之后创建的文本文件,c++,datetime,time,text-files,time-t,C++,Datetime,Time,Text Files,Time T,首先,我对X++的了解很少,我只需要编辑给我的代码。 我有一个C++程序,它创建文本文件并在其中存储数据。目前,该程序正在使用: outfile.open("C:/Users/Admin/Documents/MATLAB/datafile.txt", std::ios::app); 但我需要更改此代码,以便每次运行此代码时,它都会创建一个新的文件名。我的建议是以某种方式合并时间/日期作为文件名,但我不确定如何做到这一点。我试着四处研究,似乎使用time\t是一种方法,但我不确定如何在我的案例中

首先,我对X++的了解很少,我只需要编辑给我的代码。 我有一个C++程序,它创建文本文件并在其中存储数据。目前,该程序正在使用:

outfile.open("C:/Users/Admin/Documents/MATLAB/datafile.txt", std::ios::app);
但我需要更改此代码,以便每次运行此代码时,它都会创建一个新的文件名。我的建议是以某种方式合并时间/日期作为文件名,但我不确定如何做到这一点。我试着四处研究,似乎使用
time\t
是一种方法,但我不确定如何在我的案例中使用它

是否可以将时间/日期保存为变量,然后使用:

outfile.open("C:/Users/td954/Documents/MATLAB/<DEFINED VARIABLE>", std::ios::app);
//                                            ^^^^^^^^^^^^^^^^^^
outfile.open(“C:/Users/td954/Documents/MATLAB/”,std::ios::app);
//                                            ^^^^^^^^^^^^^^^^^^
如果是的话,我会怎么做


谢谢大家

当然,你们可以这样做:

// Calculate the path
time_t current_time = time(nullptr);
std::ostringstream path_out(prefix);
path_out << "-" < < current_time << ".txt";
std::string path = path_out.str();

// Open a file with the specified path.
std::ofstream out(path.c_str(), std::ios::app);

// do stuff with "out"
std::time_t t = std::time(NULL);
std::tm *ptm = std::localtime(&t);
std::stringstream ss;
ss << ptm->tm_year+1900 << std::setw(2) << ptm->tm_mon+1 << ptm->tm_mday << ptm->tm_hour+1 << ptm->tm_min+1 << ptm->tm_sec+1;
outfile.open(("C:/Users/Admin/Documents/MATLAB/datafile_"+ss.str()+".txt").c_str(), std::ios::app);
//计算路径
时间=当前时间=时间(空PTR);
std::ostringstream路径输出(前缀);

path_out您可以使用通过C++11的日期和时间功能提供的时间戳。特别是
std::chrono
命名空间的
now()
函数将返回您想要的:

#include <chrono>

std::ostringstream oss;
oss << "myfile" << std::chrono::system_clock::now() << ".txt";
//                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

std::ofstream out(oss.str(), std::ios_base::app);
#包括
std::ostringstream oss;
oss
#包括
#包括
#包括
std::string getTimestampedFilename(const std::string和basePathname){
std::ostringstream文件名;

文件名尝试以下操作:

// Calculate the path
time_t current_time = time(nullptr);
std::ostringstream path_out(prefix);
path_out << "-" < < current_time << ".txt";
std::string path = path_out.str();

// Open a file with the specified path.
std::ofstream out(path.c_str(), std::ios::app);

// do stuff with "out"
std::time_t t = std::time(NULL);
std::tm *ptm = std::localtime(&t);
std::stringstream ss;
ss << ptm->tm_year+1900 << std::setw(2) << ptm->tm_mon+1 << ptm->tm_mday << ptm->tm_hour+1 << ptm->tm_min+1 << ptm->tm_sec+1;
outfile.open(("C:/Users/Admin/Documents/MATLAB/datafile_"+ss.str()+".txt").c_str(), std::ios::app);
std::time\u t=std::time(空);
std::tm*ptm=std::localtime(&t);
std::stringstream-ss;
ss tm_年+1900以下代码:

time_t currentTime = time(0);
tm* currentDate = localtime(&currentTime);
char filename[256] = {0};

strcpy(filename, "C:/Users/Admin/Documents/MATLAB/datafile");
strcat(filename, fmt("-%d-%d-%d@%d.%d.%d.txt",
       currentDate->tm_hour, currentDate->tm_min, currentDate->tm_sec,
       currentDate->tm_mday, currentDate->tm_mon+1,
       currentDate->tm_year+1900).c_str());

outfile.open(filename, std::ios::app);
tm*和time_t位于ctime头中。fmt的功能与sprintf类似,后者格式化字符串。实现(不是我的,在stackoverflow IIRC上找到):

#包括
std::string fmt(const std::string和fmt,…){
int size=200;
std::字符串str;
va_列表ap;
而(1){
str.resize(大小);
va_启动(ap、fmt);
int n=vsnprintf((char*)str.c_str(),size,fmt.c_str(),ap);
va_端(ap);
如果(n>-1&&n-1)
尺寸=n+1;
其他的
尺寸*=2;
}
返回str;
}

使用这种方法的另一个好处是,您使用的是一个可重用的函数,它清楚地说明了它在做什么。这应该是您编写的任何软件的目标。在以下行中:std::ostringstream path_out(prefix);prefix是什么意思,我为它获取了未定义的标识符。谢谢我假设您有一个名为“prefix”包含“C:\…”(用该路径的其余部分替换“…”)。我尝试实现了您建议的代码,但我得到了错误“fmt”:找不到标识符。是否需要为该函数包含一个标头查看整个帖子…我给了您fmt的代码。这是第二个代码。只需复制第二个代码(fmt函数定义),然后放入#include,然后在它下面放入第一个代码。
#include <cstdarg>
std::string fmt(const std::string& fmt, ...) {
    int size = 200;
    std::string str;
    va_list ap;
    while (1) {
        str.resize(size);
        va_start(ap, fmt);
        int n = vsnprintf((char*)str.c_str(), size, fmt.c_str(), ap);
        va_end(ap);
        if (n > -1 && n < size) {
            str.resize(n);
            return str;
        }
        if (n > -1)
            size = n + 1;
        else
            size *= 2;
    }
    return str;
}