使用日期和时间命名输出文件

Naming output file with date&time

本文关键字:输出 文件 时间 日期      更新时间:2023-10-16

我是编程初学者。我做了一个人脸检测项目(检测人脸,记录结果,然后将其保存到输出文件中)。我宁愿用日期来命名它的结果;时间比使用"MyVideo"要长。但我不知道怎么做,有人能帮我吗?谢谢b4。

这是我的密码。

void rekamwajah(){
            double nBaris = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
            double nKolom = cap.get(CV_CAP_PROP_FRAME_WIDTH);
            cv::Size frameSize(static_cast<int>(nKolom), static_cast<int>(nBaris));
             if (!rekam.isOpened()) //if not intialize the VideoWriter successfully
                 {
                  rekam.open ("K:\MyVideo.avi", CV_FOURCC('M','J','P','G'), 20,   frameSize, true);
                   }
                 bool bSuccess = cap.read(framewarna); 
                 if (!bSuccess) //if not success, break loop
                 {
                     MessageBox::Show("ERROR: Cannot read a frame from video file");
                     return;
                 }
                    rekam.write(framewarna); //writer the frame into the file
                    timer1->Enabled = true;

            }

您需要的是将日期和时间作为字符串,然后将此字符串用作文件名。你可以在这里找到一个例子:https://stackoverflow.com/a/16358111/137261

#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);
    std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}