Hi im writing a function in a program that lets the user delete a specific record in a file, the user is also able to recover this record. The easiest way i found is when the record is deleted, the record will be set to deleted using bool. Then in my print function it will only print the records that are marked "deleted = false". If the user wants to recover the record it will set the deleted = false and print the record again. But i can't find a way to implement this.

So any ideas on how to delete a record from a file? And able to recover it later. Ive searched through online fstream tutorials and can't find any. The only one was quite helpful was to delete the contents of the file, make the modifications, then reprint the file. But how am i suppose to recover the record later? If i print the read record in delete() it prints the record perfectly.

Heres my code:

void delete() 
	fstream DeleteFile;
        PersonStruct tmp1;
	bool deleted = false;
	int recordPos;
        int pos;
	DeleteFile.open( "H:\\FILE", ios::binary | ios::in | ios::out );

	cout << "Delete Record: ";
	cin >> recordPos;
	pos = position(recordPos);
        if (pos == -1)
                cout << "file does not exist";
                DeleteFile.seekp(0, ios::beg); //set pointer to start of file
                DeleteFile.seekp(pos * sizeof(tmp1), ios::beg); //set pointer to the position of the record
                DeleteFile.read((char*) &tmp1, sizeof(tmp1)); //read the record from the start to the end of the record
int position(int recordPos)
	fstream SearchFile;
        PersonStruct tmp1;
        int pos = 0;

	SearchFile.open( "H:\\FILE", ios::binary | ios::in | ios::out );

	while ( !SearchFile.eof())
		SearchFile.read((char*) &tmp1, sizeof(tmp1));
		if ( tmp1.pos == recordPos)
			return pos;
	return -1;

I agree with Ancient Dragon. Think about how data recovery software works with computers. When files are deleted they get flagged as deleted so that the computer can overwrite the data stored in that area of memory. So long as the memory location hasn't already been re-used the data is still accessible.

@Ancient Dragon, I know but how do i implement this? This has been bugging me for days... there seems to be no tutorial on this on the web.

My print function just prints all contents in the file
while (!OpenFile.eof())
Print record
Read next record

>> I know but how do i implement this

Simple, just look at the delete flag

while (Read next record)
   if( delete flag not set)
      Print record