首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C++根据当前时间自动生成新的CSV文件

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

Stack Overflow用户
提问于 2019-05-16 11:30:59
回答 1查看 343关注 0票数 0

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

我试着用(struct tm)for(;timeinfo->tm_min < 60;)制作一个60年代以后生产的新的CSV。但每次循环时,项目Data、time、Channel都会再次写入CSV。看起来很奇怪。但是,当我创建文件dest并将dest.open放在for循环之外时,它只存储正确的数据格式,而没有创建我想要的新CSV。请建议我如何让它像预期的那样工作。

代码语言:javascript
复制
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;
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-05-16 12:14:27

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

代码语言:javascript
复制
#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';
}

我不知道你想用剩下的代码做什么,所以...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56160398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档