[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

More on FILE streams

Im back again,
Is it possible to REMOVE or REMOVE and REPLACE an entry from a stream (either a structure or text).
ie:
If I have saved 10 structures (of the same format) but would like to remove one of them.

or

if i have a few lines of text, I would like to remove a line or a word.
Thanx in advance
dsch
0
dsch
Asked:
dsch
  • 3
1 Solution
 
nietodCommented:
Read from the stream and write what you read to a new stream, but don't write out the things you want to delete.  Then use the new stream instead of the old.
0
 
PaullkhaCommented:
Read the file and write a new one, Huh!

I can barely remember my CS courser, but here goes.
If the structure has a fixed size:
To Delete:
1.Open the file in random access
2.Read the last structure (structure #N)
3.Overwrite the structure you want to delete (structure #3)
4.Mark the last structure as open (deleted).
To Write:
1. Open the file in random access.
2. Traverse to the end.
3. Read backwards until you find the last open structure.

YOU DO NOT WANT TO READ A 50MEG FILE THAN WRITE OUT A 50MEG FILE TO DELETE 100KB OF DATA!

Even if the structure is not fixed, simply mark the structure as deleted. When the file contains say 30% invalid structures, than do as neitod says.

There are better ways than I what I have listed here. But college was too long ago.
0
 
nietodCommented:
I don't think you can do random access on a file stream.  (note stream, not just a regular file).
0
 
dschAuthor Commented:
Thankyou nietod & Paullkha for your comments.
I think that nietod's answer will suffice for now as my app's file stream will probably only be 100k max anyway.
>>I don't think you can do random access on a file stream.  (note stream, not just a regular file).
I will try to look this up in my documentation.
Also if you only "3.Overwrite the structure you want to delete (structure #3)" the file size will not be reduced will it? There will just be an empty space. Am I right?
Any way, I'll try reading, then writing and see  how I get on.
I may be back later.
Thanx again
dsch
0
 
nietodCommented:
There is no empty space in a file.  it is sort of like an array that has not specific size.  As you write to the array it continues to grow.  When you random access to the file (which is not possible with streams), you can make the file smaller, but removing part of the end of the file.  Thus with random access, you can shift things forward and then shrink the file to account for the items at the end that are no longer needed.  But this can't be done with streams.  With streams your only option is to copy the information to a different stream (doesn't have to be a file).  Then reopen the file stream specifying ios::trunc to clear its previous contents and then write the information back.  This is not very efficient, but it streams weren't designed with high speed performance in mind, then were designed for programmer ease.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now