簡體   English   中英

每次運行程序時,如何生成和保存新的文本文件?

[英]How can I generate and save a new text file each time program is run?

int main(int argc, char* argv[])
{
        blah blah blah
        if (newDataAvailable) {
            std::ofstream outfile;                                                                              
            outfile.open("C:/Users/admin/Documents/MATLAB/afile2.txt", std::ios::app);                  
            outfile << "Blah blah blah" << "\n";
            outfile.close();

此代碼幾乎從傳感器獲取數據並將其輸出到文本文件。 “ if”語句將每10毫秒循環一次。

我需要做的是在“ if”循環之前,我需要創建一個唯一的文件名,然后一旦代碼到達if循環,它將能夠outfile.open("C:.../**FILENUMBER X.txt**",std::ios::app);

換句話說,每次運行該代碼時,我都需要此代碼來創建新的文件名。 我能想到的僅有的兩種方法是使用某種東西生成一個隨機數或使用某種東西生成日期/時間。 (但是請注意,在運行if循環時,它需要創建/打開相同的文本文件。僅當代碼停止並再次運行時,才創建新的文本文件名)

我幾乎不了解c ++編程(我只需要編輯已有的預制代碼),而我研究過的所有解決方案對我來說都沒有多大意義,所以我認為id來了。

根據第一個建議:

int main(int argc, char* argv[])
{
    //blah blah blah
    bool started = false;
    std::ofstream outfile;
    while(true) {//loop, could be while or for or whatever
      sleep(10); //you said it runs every 10ms

      if(!started){
          int fileno = 1;
          bool success = false;
          while(success &&fileno < MAX_INT) {
               ifstream ifs("C:/Users/admin/Documents/MATLAB/afile" + fileno  + ".txt", std::ios::read);// attempt read file to check if it exists
               success = ifs.good();
               ifs.close();
               fileno++;//increase by one to get a new file name
          }               
          outfile.open("C:/Users/admin/Documents/MATLAB/afile" + fileno  + ".txt", std::ios::app);
          started = true;
      }

       if (newDataAvailable) {


        outfile << "Blah blah blah" << "\n";

    }
 }
        outfile.close(); //moved out of loop so that it is not closed
return 0;

}

您可以在文件名后加上代表序列或文件編號的后綴。 例如,您將第一個文件命名為“ yourFileName1”,第二個文件命名為“ yourFileName2”,依此類推。您需要將最后一個文件的順序存儲在文件中。 首次啟動程序時,將文件的序列設置為1。下次運行程序時,從序列文件“ sequenceFile.txt”中讀取最后一個序列,並將其遞增1。程序,您應該將最后一個序列存儲回序列文件中。

#include <fstream>
#include <string>
using namespace std;

int main()
{
    unsigned int fileSeq;
    ifstream seqFileIn;
    ofstream seqFileOut;

    seqFileIn.open("sequeceFile.txt", ios::in);

    // If "sequenceFile.txt" exists, read the last sequence from it and increment it by 1.
    if (seqFileIn.is_open())
    {
        seqFileIn >> fileSeq;
        fileSeq++;
    }
    else
        fileSeq = 1; // if it does not exist, start from sequence 1.


    // Assume newDataAvailable = true
    bool newDataAvailable = true;

    if (newDataAvailable) {
        ofstream afile;
        string fileName = "afile" + to_string(fileSeq) + ".txt";

        cout << "File Name is " << fileName << endl;

        afile.open(fileName, ios::app);
        afile << "Blah blah blah! File sequence is " << fileSeq << "\n";
        afile.close();
    }

    // Before you exit your program, do not forget to store the last file sequence in "sequeceFile.txt".
    seqFileOut.open("sequeceFile.txt", ios::out);
    seqFileOut << fileSeq;

    return 0;
}

您可以使用日期作為文件名。 獲取當前日期和時鍾,然后添加您的文件名(將afile2替換為日期)。 我認為有很多不同的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM