How to get data from a file.txt to my object and then store it in mysql database in c++

Viewed 44

I am new to c++ and I have to work on a project for my end-of-study internship. The project consists in managing the data of the electronic log of the ATMs.

My problem is that I have to get the data from a file.txt(which is an electronic gab log) and store it in the table I created.

Here is an example of the file.txt (the electronic gab log) :
Every events of this file is separated by : ************************

File log.txt File log.txt

The data I need to store in the table is : (look the image)

Table journal_contents

I have the Journal object that I created and I want to get tha data in this object and then in the database.

Here is the code I've done : This is my header.h

//Declaration de struct
struct events {
    int id_event;
    std::string id_event_ligne;
    float amount_withdraw;
    int type_transaction;
    int type_return_transaction;
    int type_card_returned;
    int type_card_insert;
    int type_return_motif;
};
class Journal  // Object journal
{
public:
    Journal(){};
    int getId_ligne();
private:
    int id;
    int id_atm;
    int id_ligne;
    int date_ligne;
    int date_insert;
    std::vector<events> trans_events;

};

This is my file.cpp

//Some random code 
int Journal::getId_ligne( )
{
    time_t t =time(0);   // get time now
    tm* now = localtime(&t);
    int year= (now->tm_year + 1900);
    string syear(to_string(year));
    int month = (now->tm_mon + 1);
    string smonth(to_string(month));
    int day = now->tm_mday;
    string sday(to_string(day));
    int hour = now->tm_hour;
    string shour(to_string(hour));
    int minute = now->tm_min;
    string sminute(to_string(minute));
    int second = now->tm_sec;
    string sseconde(to_string(second));
    if(smonth.length()== 1)
    {
        smonth = '0' + smonth;
    }
    if(sday.length()== 1)
    {
        sday = '0' + sday;
    }
    if(shour.length()== 1)
    {
        shour = '0' + shour;
    }
    if(sminute.length()== 1)
    {
        sminute = '0' + sminute;
    }
    if(sseconde.length()== 1)
    {
        sseconde = '0' + sseconde;
    }
    string current_time =syear + '-' + smonth + '-' + sday + '\t' + shour + ':' + sminute + ':' + sseconde ;
    string datee= syear + smonth+ sday + ".jrn" ;
    const char* path = "C:\\Users\\dell\\Desktop\\Stage\\journals\\";
    string str_path = string(path)+ datee;
    //find filename
    unsigned found = str_path.find_last_of("/\\");
    string same_path = str_path.substr(found+1);
    //Compare
    if (datee.compare(same_path)== 0)
    {
 // Here I just writed 8902 as an Id to test but what I need to do is to get the id from the file.txt

    string filename = string(str_path);
    ifstream file1(filename);
    string line;
    string word;
    if(file1.is_open())
    {  int k=0;
        while (getline(file1, line))
        {
            if (line.find("8902") != string::npos){
            istringstream isstream;
            isstream.str(line);
            getline(isstream, word, ' ');
            getline(isstream, word, ' ');
             int id_ligne = stoi(word);
             Db_response::Connection();
            string insert_query = "INSERT INTO `journal_contents`(`id_ligne`) VALUES ('"+word+"')";
    //int qstate = mysql_real_query(conn,insert_query.c_str(), strlen(insert_query.c_str()));
    query_state=mysql_query(conn, insert_query.c_str());
    if(!query_state)
    {

        cout << "Query Execution Problem " << mysql_errno(conn) << endl;
    }
    else
    {
         cout << endl << "success" << endl;
    }
            k=1;
            }}
            if (k==0) {
            }
        file1.close();
    }
    else
        cout <<"File not found " << endl;

    file1.close();
    //cout << id << endl;
    return id_ligne;
    }
}

main.cpp

int main(int argc, const char* argv[])
{
    cout << "Hello world!" << endl;
     Journal current_journal;
     cout << current_journal.getId_ligne() << endl;

}

I hope I have explained my problem well. I really need your help Thanks in advance for helping me.

0 Answers
Related