#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include "mstruct.h"

void main()
{ int fkey;
  char q;
  long pos;
  char *ptr;
  Mstruct temp;
  filebuf f1,f2;

  cout<<"Введите значение поля fint для поиска:"; cin>>fkey;
  if (f1.open("f1.str",ios::binary|ios::in)==0)
		error("File error!!!");
	istream from(&f1);
	do
	{
	from.read(ptr,sizeof(Mstruct));
	if (from.eof()) break;
	temp=(*((Mstruct *)ptr));
	}
	while (!from.eof() && (temp.fint!=fkey));

	if (temp.fint==fkey)
	{   cout<<"Поле с ключом fint="<<temp.fint<<" найдено"<<endl;
		cout<<temp;
		pos=from.tellg()-sizeof(Mstruct);
		cout<<"Скорректировать или удалить запись (y,n)";
		cin>>q;
		if (q=='y')
		{
		cin>>temp;
		f1.close();
			if (f1.open("f1.str",ios::binary|ios::out)==0)
				error("File error!!!");
				ostream to(&f1);
				to.seekp(pos);
				to.write((char *)(&temp),sizeof(Mstruct));
		}
		else
		{ if (f2.open("f2.str",ios::binary|ios::out)==0)
						error("File error!!!");
						ostream to(&f2);
		  from.seekg(pos+sizeof(Mstruct));
		  do
		  {   from.read(ptr,sizeof(Mstruct));
			if (from.eof()) break;
			to.write(ptr,sizeof(Mstruct));
		  }
		  while (!from.eof());
		  f2.close();
		  remove("f1.str");
		  rename("f2.str","f1.str");
		  }
	}
	f1.close();
		if (f1.open("f1.str",ios::binary|ios::in)==0)
		error("File error!!!");
	istream fr(&f1);
	cout<<"Вывод файла\n";
	do {
	fr.read(ptr,sizeof(Mstruct));
	if (fr.eof()) break;
	cout<<(*((Mstruct *)ptr));
	}
	while (!fr.eof());
	f1.close();
}