> how can i delete some block from .txt file...?
Having read your most recent reply (about structures), and that you also mention fwrite, can you be clear about what sort of file you actually have?
A text file consists of
- printable characters and whitespace (newline, tabs).
- variable length records.
The usual way of reading this kind of file is using fgets()
There is no good way to delete a line without creating a whole new file, and copying all the lines you want to keep.
A binary file consists
- any representable character.
- fixed length records, often mapped directly to a 'C' structure.
The usual way of reading this kind of file is using fread()
If you want to delete a record, then you have the same choice as for a text file.
You also have the choice of having something like
Code:
struct filerec {
char isDeleted;
// other data
};
Here, you can just rewrite the record with the 'isDeleted' flag set to 1.
Advantages:
1. Is is very quick.
2. Adding a new record can be achieved by finding an old record with 'isDeleted' and overwriting it with new data.
Disadvantages:
1. The file takes up a lot more space, especially if it contains a lot of deleted records. This can be mitigated by running a 'compact' on the database during any kind of maintenance cycle.
2. Everything which reads records needs to check the 'isDeleted' flag before deciding what to do with the record.
--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.