My sample text file look something like this:
Name: First
Email: first@gmail.com
Name: Second
Email: second@gmail.com
Currently I wrote a function to read a record from specified binary file:
Staff getARecord (fstream& afile, const char fileName [], int k)
{
afile.open (fileName, ios::in | ios::binary);
Staff s;
afile.seekg ((k - 1) * sizeof (Staff), ios::beg);
afile.read (reinterpret_cast <char *>(&s), sizeof (s));
afile.close ();
return s;
}
Staff
is a structure consist of name
and email
field. Then I will get the record based on the user input:
int k;
cout << "Enter your email: ";
cin >> k;
Staff s = getARecord(afile,"staff.dat",k);
Then I've successfully read the data if user's input is numeric(1 and 2 for now since I only have 2 records) for the sake of seekg
function, how can I retrieve the same result if user input the email instead of record number?