I am currently using fseek()
in program. I have also maintained an array of bytes which keeps the track of bytes of each line that is printed inside the file.I am also using a segment tree, so that the access to a particular position is faster. Also, I am not allowed to use anything else...(like structs)
Is there a way to do so?

- 52,653
- 6
- 59
- 97

- 522
- 1
- 5
- 15
-
Do you want to physically delete the record? – alk Jan 18 '15 at 11:21
-
yes i want to delete the record from the file – Palash Ahuja Jan 18 '15 at 11:58
-
If you can, overwrite the record with invalid, automatically skipped data. For example, in a text file, that could be a comment line, or perhaps just spaces followed by a single newline. At regular intervals (after deleting sufficient number of records), you regenerate the file by reading old data, but writing only the valid records. (If you open the file twice, you can do it in-place.) While you could remove each record by copying the tail part of the file over the record-to-be-deleted, it's slow. Again, open the file twice to avoid having to `fseek()` between reads and writes. – Nominal Animal Jan 18 '15 at 15:25
1 Answers
Maybe there are better options than this but you can do something like this
store the position pointer from where you wish to delete. // variable1 store in another variable the seek position from where the delete operation must end.
now from the position where the delte must end till end of file, read the values into a buffer. Now again seek back to variable1 and start appending from that position the contents stored in buffer. Ultimately add EOF once the append operation is complete.
Easier method but even more memory/time intensive: read the whole file into a buffer. now replace the contents into buffer. again open the same file but in "write" mode and empty the buffer into file. This will over write everything without creating a new file.
Hope someone puts forth a better solution :)

- 843
- 6
- 20