'Function to parse automaticly files .txt after 24 hour in c++

I have a project and I'm new to C++ Well I have to parse a file.txt that contains transaction informations in an ATM. And there is files named as the date of the transactions. For example the file (20211102.txt) is a file that has all the informations, created in 02/11/2021. So after midnight another file is being created . !! My Objectif is : Every day I have to parse the file from the day before. I have tried a function that generate the date of yesterday. I have to use it But I don"t know how

-Here is the function of parsing the file

void parse()
{
    ifstream file1("20211120.jrn");
    string str;
    if(file1.is_open())
    {
        while (getline(file1, str))
        {
            cout << str << "\n";
        }
        file1.close();
        cout << "PARSING FIRST JOURNAL" << endl;
    }
    else
        cout <<"File not found " << endl;

    file1.close();
}

I hope it's clear . Thank you in advance.



Solution 1:[1]

As discussed in this question, you can do something like:

#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string>
#include <iostream>

std::string return_current_time_and_date() {
    using namespace std::chrono_literals;
    auto now = std::chrono::system_clock::now();
    auto in_time_t = std::chrono::system_clock::to_time_t(now - 24h);

    std::stringstream ss;
    ss << std::put_time(std::localtime(&in_time_t), "%Y%m%d");
    return ss.str();
}

where some things have been adapted for your use case (i.e. format and so the date is of yesterday). See operation here.

As to running it every day you'd have to have some scheduler run it for you. I am not that familiar with this on windows, but this website seems to offer good advice on how you can set up such a job. Alternatively this question, might be of use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lala5th