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.