2014-02-17 56 views
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); 
//           ^^^^^^^^^^^^^^^^^^ 

如果是這樣,我將如何去這件事嗎?

謝謝你們

回答

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-%[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這樣的格式化字符串的函數。實現(不是我的,在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; 
} 
+0

我試着實現你所建議的代碼,但是我得到錯誤'fmt':找不到標識符。有一個頭,我需要包括該功能 – user3131101

+0

看看整個帖子...我已經給你的fmt的代碼。這是第二個代碼。 – Ezo

+0

只需複製第二個代碼(fmt函數定義),然後把#include ,然後在它下面放第一個代碼。 – Ezo

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上執行此操作的首選方式,但是誰在乎;))。

+0

在行中: std :: ostringstream path_out(prefix); 前綴是什麼意思,即時獲取未定義的標識符。 謝謝 – user3131101

+0

我假設你有一個名爲「prefix」的字符串變量,其中包含「C:\ ...」(將該路徑的其餘部分替換爲「...」)。 –

0

您可以使用通過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); 
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."); 
    } 
} 
+0

使用這種方法的另一個好處是你使用了一個可重用的函數,清楚地說明它在做什麼。這應該是您編寫的任何軟件的目標。 – Andrew

-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); 
相關問題