in my lab work we gotta read datas from txt and write them to .dat file as a binary format, then we should search and find datas by given id in binary file.
Our worker.txt file in this
format:
ID firstname lastname age salary
like:
341 Alex Morgan 32 3400
234 Jessica Gibson 28 2000
...
Program reads these datas then writes all of them to worker.dat as a binary file.
The problem is when entered valid Id for searching program find and print it but then close with windows stopped execution error, I need your help to handle it.
My searchByID function:
void searchByID( int key )
{
bool find=false;
int id,age,sal;
string fn,ln;
ifstream InFile;
InFile.open("worker.dat", ios::in | ios::binary);
int i = getSize( initialArr );
int j;
for( j=0; j<i; j++ ){
InFile.read( (char*)&id, sizeof(int));
InFile.read( (char*)&fn, sizeof(string));
InFile.read( (char*)&ln, sizeof(string));
InFile.read( (char*)&age, sizeof(int));
InFile.read( (char*)&sal, sizeof(int));
if( id == key )
{
cout << "Datas for Entered ID:\n"
<< id <<" "<< fn <<" "
<< ln <<" "<< age <<" "
<< sal << endl;
find = true;
}
}
if( !find )
cout << "Entered ID not Found on File" << endl;
InFile.close();
}
worker.txtbut not the format ofworker.dat