c++ - 如何使用C++根据当前时间自动生成新的CSV文件

标签 c++ csv clock

我已经完成了读取传感器数据并成功存储到 CSV 文件中的任务。但是每次我想停止读取和存储传感器数据时,我都必须关闭 exe 文件。由于工厂运作,他们希望在不关闭exe文件的情况下连续运行一个月的程序。我被要求在一段时间后(例如 1 小时)修改代码以生成新的 CSV 文件。所以文件名可能是:20190516_10h0m0s,20190516_11h0m0s,20190516_12h0m0s ...

我尝试使用 (struct tm)for(;timeinfo->tm_min < 60;)制作 60' 后生成的新 CSV。但是每次循环,项目数据、时间、 channel 都会再次写入 CSV。看起来很奇怪。但是当我创建文件 dest 并将 dest.open 放在 for 循环之外时,它只会存储正确的数据格式,而不会根据需要创建新的 CSV。请建议我如何使其按预期工作。

void EX_GetMultiValue(LONG i_lDriverHandle,
                      WORD i_wSlotID,
                      struct SlotInfo &i_SlotInfo)
{
    char filename[20], filename2[20];
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    timeinfo->tm_mon++; // Because the range of tm_mon is 0~11, so we need to increment it by 1
    timeinfo->tm_year = timeinfo->tm_year + 1900; // Because year counted since 1900

    clock_t start = clock();
    sprintf(filename,
            "%04d%02d%02d_%02dh%02dm%02ds.csv",
            timeinfo->tm_year,
            timeinfo->tm_mon,
            timeinfo->tm_mday,
            timeinfo->tm_hour,
            timeinfo->tm_min,
            timeinfo->tm_sec);
    printf("\nFilename: %s", filename);

    ofstream dest2;
    dest2.open(filename, ios_base::app | ios_base::out);
    dest2 << "Date" << "," << "Time" << "," << "milisecond" << ","
            << "Channel 0" << "," << "Channel 1" << "," << "Channel 2" << ","
            << "Channel 3" << "," << "Channel 4" << "," << "Channel 5" << ","
            << "Channel 6" << "," << "Channel 7" << "," << "Channel 8" << ","
            << "Channel 9" << "," << "Channel 10" << "," << "Channel 11" << ","
            << endl;

    for (; timeinfo->tm_min < 60;)
    {

        ofstream dest;
        dest.open(filename, ios_base::app | ios_base::out);

        LONG lGetMultiValueResult = AIO_GetValues(i_lDriverHandle,
                                                  i_wSlotID,
                                                  wRawValue); //get raw value
        if (ERR_SUCCESS == lGetMultiValueResult)
        {
            clock_t timeElapsed = clock() - start;
            unsigned secElapsed = timeElapsed / CLOCKS_PER_SEC;
            unsigned msElapsed = timeElapsed / CLOCKS_PER_MS;

            while (msElapsed >= 1000)
                msElapsed -= 1000;
            while ((timeinfo->tm_sec + secElapsed) > 59)
            {
                timeinfo->tm_sec -= 60;
                timeinfo->tm_min++;
            }
            while (timeinfo->tm_min > 59)
            {
                timeinfo->tm_min -= 60;
                timeinfo->tm_hour++;
            }
            while (timeinfo->tm_hour > 23)
            {
                timeinfo->tm_hour -= 24;
                timeinfo->tm_mday++;
            }

            dest << timeinfo->tm_year << "-" << timeinfo->tm_mon << "-"
                    << timeinfo->tm_mday << "," << timeinfo->tm_hour << "h"
                    << timeinfo->tm_min << "m" << timeinfo->tm_sec + secElapsed
                    << "s" << ",";
            dest << msElapsed << "ms" << ",";
            for (iCnt = 0; iCnt < g_ChannelNum; iCnt++)
            {
                wRangeType = *(i_SlotInfo.wChRange + iCnt); //get range type
                EX_ScaleRawValue(wRangeType,
                                 wRawValue[iCnt],
                                 &dScaledValue,
                                 cUnit); //get scale value            
                if (strcmp(cUnit, "UN") != 0)
                {
                    printf("Channel %d raw data is 0x%04X, scaled value is %.4f %s.\n",
                           iCnt,
                           wRawValue[iCnt],
                           dScaledValue,
                           cUnit);
                    dest << dScaledValue << ",";

                    Sleep(1);
                }
                else
                    printf("Channel %d range is unknown.\n", iCnt);
            }
            dest << endl;
        }
        else
            printf("Fail to get value, error code = %d\n",
                   lGetMultiValueResult);
        dest.close();
        dest2.close();

        if (dest == NULL)
        {
            perror("Error creating file: ");
            return;
        }
    }

最佳答案

使用标准 C++ 工具和 Howard Hinnats date.h :

#include <chrono>
#include <string>
#include <iostream>

#include "date.h"

int main()
{
    auto now{ std::chrono::system_clock::now() };
    std::string filename{ date::format("%Y%m%e_%Hh%Mm%Ss.csv", now) };
    std::cout << filename << '\n';
}

我不知道您要对其余代码做什么,所以...

关于c++ - 如何使用C++根据当前时间自动生成新的CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160398/

相关文章:

c++ - “std::filesystem”在包含 <experimental/filesystem> 后未声明

csv - 使用 Jet Oledb 读取文本文件,标题关闭(HDR=否)

node.js - 在node js中获取mongodb集合数据到csv

c - 一毫米:ss timer C

c++ - 无法打印斐波那契数列

c++ - 无法初始化 512x512 数组

PHP 将大型 CSV 文件导入 MySQL 表

c++ - 是否确保 2 个连续的 std::chrono::steady_clock::now() 不相等?

java - 如果时钟更改/调整,我可以收到通知吗?

c++ - 如何在用户想要的时候一直阅读输入?