acctually my program works like this...
if i want to delete a data in the middle or else it copies the last data over the one we want to delete and it fills the last data with "\0"... and... i didnt wait for the answer and i add new modification and it use over write to delete... but as i understand SetEndOfFile(); set EOF where i want? and its in C or C++? i need C
anyway thx...
Why not read line by line or block by block (saving memory that way) from the original file, and write only the needed lines or blocks into new file. Then delete original file, and rename the new file.. Seems a better decision to me, than a filepos jumping or reading a big chunk of file data into memory at once.
im reading block by block (helps of structers) and my text file like database...i need to delete some entries and for that way i change the block that i want to delete with the last block but last block become empty (just spaces) so.. i think that if i delete this block it might be great... so i asked...anyway i didnt read whole file...
> 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.
You could also create another index file that contains a map of the database file and tells you where available slots are to insert a new record. If your records have a searchable key, then this method would also greatly improve search performance since you don't need to read every record just to find the one you want. Just find it in the index file, and that index should contain the position of the record...
@Salem according to your words i have binary files mean fixed lenght and structer ...
acctually after i asked that question i found a way to do but i wanted to continue for this topic cuz i wanted to learn if there is something i dont know...
thx for all answers...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.