0

Hello Everyone I am new to c++ . I want to get pecific data from a file.txt and store it in my object

This is an example of the file : file.txt1

For example the variable id_atm is 7154 in this file , so I want to get that id . There's a lot of other ids so I want to get them from that file to my object Journal but I don't know how to do it

Here is the code that I writed :

file.h (header)

//Declaration de struct
struct events
{
    int id_ligne;
    int id_event;
    int date_ligne;
    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 to define the properties
class Journal
{
// Instance variables
private:
    int id;
    int id_atm;
    int date_insert;
    std::vector<events> trans_events;
public:
    static void Connection();
    int output_data();
};

file.cpp

int Journal::output_data()
{
    // Object to read from file
    ifstream file_obj;

    // Opening file in input mode
    file_obj.open("C:\\Users\\dell\\Desktop\\Stage\\journals\\20220602.jrn", ios::in);
    // Object of class contestant to input data in file
    Journal obj;
    // Reading from file into object "obj"
    file_obj.read((char*)&obj, sizeof(obj));
    // Checking till we have the feed
    string line;
    string word;
    if(file_obj.is_open())
    { int k=0;
        while (getline(file_obj, line))
        {
            if (line.find("7154") != string::npos) // I gave an example 7154 but what I want is to //find that 7154 in the file 
            {
                istringstream isstream;
                isstream.str(line);
                getline(isstream, word, ' ');
                getline(isstream, word, ' ');
                obj.id_atm = stoi(word);
                k=1;
            }
        }
        if (k==0) {
                cout << "No ID " << endl;
            }
            file_obj.close();
    }
    else
        cout <<"File not found " << endl;
        file_obj.close();
        cout << obj.id_atm << endl;
    return obj.id_atm;
}

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

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
  • That's a text file. You can't read it directly into an object. – molbdnilo Jun 02 '22 at 11:30
  • `file_obj.read((char*)&obj, sizeof(obj))` => You are treating your `Journal` object as a `char` array (which it obviously isn't). – Wais Kamal Jun 02 '22 at 11:32
  • Does this answer your question? [Serializing a class which contains a std::string](https://stackoverflow.com/questions/7046244/serializing-a-class-which-contains-a-stdstring) – Stephen Newell Jun 02 '22 at 11:35
  • @WaisKamal and what can I do instead of char? – Hajar lyoubi Jun 02 '22 at 11:35
  • @StephenNewell Actually I'll need the deserializing cause I have data in file and I must store it in my object – Hajar lyoubi Jun 02 '22 at 11:38
  • You need to specify where *in your object* you would like to read the file. If you want to read the file directly into your object, define a char array in your class and use `file_obj.read((char*)&obj., sizeof(obj.))`. Otherwise you can read the file in another array and copy the data into your object. – Wais Kamal Jun 02 '22 at 11:38
  • 1
    You need to parse the text in the file and extract the parts you want. – molbdnilo Jun 02 '22 at 11:39
  • @molbdnilo I didi the parse , but I don't know how to extract the parts I want after parsing the file The code that I used to parse ` Journal getParseJournal(Journal j) { //random code while (getline(file1, str)){ cout << str << "\n"; }} else // random code ` – Hajar lyoubi Jun 02 '22 at 12:05
  • You also need to *not* do `file_obj.read((char*)&obj, sizeof(obj));`, since that will lead to undefined behaviour. – molbdnilo Jun 02 '22 at 12:06
  • You need to find a specification of the file format. It appears to be "line of asterisks, then a line with id, date, and some other identifier, then timestamped lines of events mixed with some arbitrary stuff". It won't be easy - the seemingly randomly placed "*496*" and "*497*" will make you miserable and curse the person that created it. – molbdnilo Jun 02 '22 at 12:12
  • @molbdnilo I thought to do a variable where I will generate * 496 * for example as a regex to help me find that word , but I don't know how to get this data ' id , date , and others ' at the first place – Hajar lyoubi Jun 02 '22 at 12:25
  • @molbdnilo Or each time there's * 3digit of number * I'll make sure that this is the id_event and so on – Hajar lyoubi Jun 02 '22 at 12:39
  • It seems like your file format is a bit too arbitrary for machine reading. Is there any way you could change it to be more predictable? – molbdnilo Jun 02 '22 at 12:41
  • @molbdnilo Unfortunately I can't change that file , I don't have to store all the data in this file , just the ones thatI specified in my class. " id_event" = 7154, "id_ligne" which is 469, "date_insert" which is 2021-12-19 05:00:04 for example , I just have to store some specific data that's all – Hajar lyoubi Jun 02 '22 at 12:46

0 Answers0