views:

57

answers:

2

I need to Erase the file contents from a selected Point (C++ fstream) which function should i use ?

i have written objects , i need to delete these objects in middle of the file

+7  A: 

C++ has no standard mechanism to truncate a file at a given point. You either have to recreate the file (open with ios::trunc and write the contents you want to keep) or use OS-specific API calls (SetEndOfFile on Windows, truncate or ftruncate on Unix).

EDIT: Deleting stuff in the middle of a file is an exceedingly precarious business. Long before considering any other alternatives, I would try to use a server-less database engine like SQLite to store serialised objects. Better still, I would use SQLite as intended by storing the data needed by those objects in a proper schema.

EDIT 2: If the problem statement requires raw file access...

As a general rule, you don't delete data from the middle of a file. If the objects can be serialised to a fixed size on disk, you can work with them as records, and rather than trying to delete data, you use a table that indexes records within the file. E.g., if you write four records in sequence, the table will hold [0, 1, 2, 3]. In order to delete the second record, you simply remove its entry from the table: [0, 2, 3]. There are at least two ways to reuse the holes left behind by the table:

  1. On each insertion, scan for the first unused index and write the object out at the corresponding record location. This will become more expensive, though, as the file grows.
  2. Maintain a free list. Store, as a separate variable, the index of the most recently freed record. In the space occupied by that record encode the index of the record freed before it, and so on. This maintains a handy linked-list of free records while only requiring space fo one additional number. It is more complicated to work with, however, and requires an extra disk I/O when deleting and inserting.

If the objects can't be serialised to a fixed-length, then this approach becomes much, much harder. Variable-length record management code is very complex.

Finally, if the problem statement requires keeping records in order on disk, then it's a stupid problem statement, because insertion/removal in the middle of a file is ridiculously expensive; no sane design would require this.

Marcelo Cantos
can i use ios::trunc to erase a point ?
Sudantha
No; `ios::trunc` is a flag that's used when you open a file for writing.
Marcelo Cantos
what abt then filling with blank space?
Sudantha
but using <pre><code>SetEndOfFile>i think i cannot remove content from middle ?
Sudantha
What do you mean? If you want to zero-pad a file to a certain size, then that's a completely different matter to your question as it is currently posed.
Marcelo Cantos
You also didn't clearly indicate that you wanted to remove content in the middle of a file. When you say, "erase ... from a selected point," most people understand that to mean, "... to the end of the file." You can't remove data from the middle of a file.
Marcelo Cantos
done check the question :)
Sudantha
@Sudantha: I've amended my answer accordingly.
Marcelo Cantos
@Marcelo Cantos thanks for your support but assignment required to use files :)
Sudantha
I've amended my answer again.
Marcelo Cantos
+2  A: 

The general method is to open the file for read access, open a new file for write access, read the content of the first file and write the data you want retained to the second file. When complete, you delete the first file and rename the second to that of the first.

Clifford