#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include "auto.h"

const char* file_name = "auto.dat";

long find(char* Model, fstream& fin)
{
   automobile A;
	fin.seekg(0, ios::beg);
	while (fin && strcmp(Model, A.GetModel()))
	  fin.read((char*)&A,sizeof(automobile));
	if (!strcmp(Model, A.GetModel())) {
	 long k = fin.tellg() - sizeof(automobile);
	 return k;
   }
	else return -1;
}

void change(automobile& Ob, long k, fstream& fout)
{
	fout.seekp(k, ios::beg);
	if (fout)
	  fout.write((char*)& Ob, sizeof(automobile));
	else cout << "ERROR !";
}

void main()
{
	automobile A;
	char* search_model = new char [10];
	cout<<"Enter model for changing of object: ";
	cin>>search_model;
	fstream fio(file_name,ios::binary|ios::in|ios::out);
	long k = find(search_model,fio);
	if (k!=-1){
	  fio.seekg(k);
	  fio.read((char*)& A,sizeof(automobile));
	 cout<<A;
	}
	else { cout<<"This person is absent !";exit(1); }

	cout<<"Enter new data:\n";
	cin>>A;
	change(A, k, fio);
	cout<<"You input:\n";
	fio.seekg(k);
	fio.read((char*)&A,sizeof(automobile));
	cout<<A;
}