-1

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.

  • 3
    I don't really see the question here. Are you asking how to check the time? – Marcell Juhász May 11 '22 at 11:18
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please learn how to [edit] your questions to improve them. For example what is the actual problem you have? To get the current date? Get yesterdays date? How to run your function on regular intervals? Something else? – Some programmer dude May 11 '22 at 11:18
  • 1
    If you want to automatically run it each midnight, maybe some task scheduler? Like `crontab` on linux? You'd need to automatically check the date, but that can be done with some clever formatting and `` – Lala5th May 11 '22 at 11:18
  • @Someprogrammerdude My actual problem is how to parse files each midnight automatically . Thank you for your help – Yasmine Takatart May 11 '22 at 15:01
  • @Lala5th Exactly I want to parse the file automatically each midnight. I'm working on windows. – Yasmine Takatart May 11 '22 at 15:01
  • @Lala5th How can I ccheck the date automatically? Thank you for your help – Yasmine Takatart May 11 '22 at 15:02
  • I recommend you use something like crontabs on POSIX systems (like Linux or macOS) or the Windows task scheduler to set up so your programs runs once a day. Then your program takes the current data, subtract one day, and do the work on the appropriate file. Once processing of that single file have finished, you just exit the program. – Some programmer dude May 12 '22 at 05:25
  • 1
    You haven't been clear about whether you need your _program_ to handle the part that checks for a new file, or of your program is run by some agent on the operating system. [Edit] the question and be clear about what you want to achieve and what your constraints are. –  May 13 '22 at 17:43
  • @clvrmnky I have edited my question , I hope it's clear now. – Yasmine Takatart May 16 '22 at 09:56
  • @clvrmnky I need my program to parse yesterday file's everytime a new file has come (today's file). – Yasmine Takatart May 16 '22 at 09:58

1 Answers1

0

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.

Lala5th
  • 1,137
  • 7
  • 18