Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete block from file

Status
Not open for further replies.

genesistr

Programmer
Apr 23, 2007
5
TR
hi,
how can i delete some block from .txt file...?
i dont want to over write new block/datas using fwrite...
i want to delete...
 
Read the file into memory.
Change it to the way you want it.
Write the file back to disk.
 
good idea but i need to delete that block without reading all into memory and writing again.
 
Why? So you're basically trying to artificially fragment a file?

If you want to delete data at the end of a file, you can use SetEndOfFile().

I can't see any functions that let you delete the middle of a file though. You can take a look and see if I missed anything:
 
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...
 
There aren't any classes being used in that function, so you can call it from C or C++ (or VB or...)

I believe you'd need to move the data that's after the deleted block up to overwrite the block you're deleting, then set EOF to the new EOF position.
 
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.

------------------
When you do it, do it right.
 
IMHO, dEVooXiAm's post presents the only safe and robust (professional;) decision...
 
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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top