0

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

如果是这样,我将如何处理?

多谢你们

4

5 回答 5

0

当然,您可以执行以下操作:

// 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 上执行此操作的首选方式是什么,但谁在乎;))。

于 2014-02-17T01:18:35.217 回答
0

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

#include <chrono>

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

std::ofstream out(oss.str(), std::ios_base::app);
于 2014-02-17T01:18:45.883 回答
0
#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.");
    }
}
于 2014-02-17T01:22:56.100 回答
0

这是代码:

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上找到):

#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;
}
于 2014-02-17T01:31:55.097 回答
-1

尝试这样的事情:

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);
于 2014-02-17T01:29:12.903 回答