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

问题描述:

首先,我对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); 
//           ^^^^^^^^^^^^^^^^^^ 

如果是这样,我将如何去这件事吗?

谢谢你们

这里是代码:

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-%[email protected]%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这样的格式化字符串的函数。实现(不是我的,在* IIRC上找到):

#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; 
} 
+0

我试着实现你所建议的代码,但是我得到错误'fmt':找不到标识符。有一个头,我需要包括该功能 – user3131101

+0

看看整个帖子...我已经给你的fmt的代码。这是第二个代码。 – Ezo

+0

只需复制第二个代码(fmt函数定义),然后把#include ,然后在它下面放第一个代码。 – Ezo

当然,你可以做这样的事情:

// 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" 

话虽这么说,如果你想以创建临时文件,然后使用时间为文件名您正在寻找的解决方案可能是特定于平台的。在基于UNIX的系统上,mkstemp是创建临时文件的首选方式(我不确定在Windows上执行此操作的首选方式,但是谁在乎;))。

+0

在行中: std :: ostringstream path_out(prefix); 前缀是什么意思,即时获取未定义的标识符。 谢谢 – user3131101

+0

我假设你有一个名为“prefix”的字符串变量,其中包含“C:\ ...”(将该路径的其余部分替换为“...”)。 –

您可以使用通过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); 

#include <ctime> 
#include <stdexcept> 
#include <sstream> 

std::string getTimestampedFilename(const std::string &basePathname) { 
    std::ostringstream filename; 
    filename << basePathname << '.' << static_cast<unsigned long>(::time(0)); 
    return filename.str(); 
} 

现在,你可以在此的其他地方使用...

template<class T> 
void writeToFile(const std::string &basePathname, T data) { 
    std::string filename = getTimestampedFilename(basePathname); 
    std::ofstream outfile(filename.c_str()); 

    if (outfile) { 
     outfile << data; 
    } 
    else { 
     throw std::runtime_error("Cannot open '" + filename + "' for writing."); 
    } 
} 
+0

使用这种方法的另一个好处是你使用了一个可重用的函数,清楚地说明它在做什么。这应该是您编写的任何软件的目标。 – Andrew

尝试是这样的:

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);